Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将短位转换为整数_Java - Fatal编程技术网

Java 将短位转换为整数

Java 将短位转换为整数,java,Java,我有一个TCP数据包,它发送一组无符号变量(它们是无符号的,因为它们节省了空间并使用了唯一ID的限制),我需要在java中将这些无符号的短位数据转换成整数 所以我的问题是如何将byteArray[0-15]转换为int 编辑: 以下是我更改为的代码: ByteOrder order = ByteOrder.BIG_ENDIAN; requestedDateType = new BigInteger(ByteBuffer.allocate(2).put(data, 8, 2).order(orde

我有一个TCP数据包,它发送一组无符号变量(它们是无符号的,因为它们节省了空间并使用了唯一ID的限制),我需要在java中将这些无符号的短位数据转换成整数

所以我的问题是如何将byteArray[0-15]转换为int

编辑:

以下是我更改为的代码:

ByteOrder order = ByteOrder.BIG_ENDIAN;
requestedDateType = new BigInteger(ByteBuffer.allocate(2).put(data, 8, 2).order(order).array()).intValue();
进入的数据缓冲区是:

bit   0    1   2   3   4   5   6   7   8   9

value 40   0   0   0   8   0   0   0   1   0
数据以小端点形式发送。我想既然BigInteger假设大,我需要转换成它。然而,无论是大订单还是小订单,我的回报都是一样的

我希望
requestedDateType
的值为1,但是我得到了256。如何填充缺少的两个字节,以确保它为0000 0001而不是0000 0001 0000 0000

编辑2:

没关系。将代码更改为:

ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(data, 8, 2);
int value = ((int)bb.getShort(0)) & 0xff;

在java.nio包中使用ByteBuffer

//Convert unsigned short to bytes:
//java has no unsigned short. Char is the equivalent.
char unsignedShort = 100;
//Endianess of bytes. I recommend setting explicitly for clarity
ByteOrder order = ByteOrder.BIG_ENDIAN;
byte[] ary = ByteBuffer.allocate(2).putChar(value).order(order).array();

//get integers from 16 bytes
byte[] bytes = new byte[16];
ByteBuffer buffer= ByteBuffer.wrap(bytes);
for(int i=0;i<4;i++){
    int intValue = (int)buffer.getInt();
}
//将无符号短字符转换为字节:
//java没有无符号的short。Char是等价的。
char unsignedShort=100;
//字节的结束性。为了清晰起见,我建议明确设置
字节顺序=字节顺序.BIG_ENDIAN;
byte[]ary=ByteBuffer.allocate(2).putChar(value).order(order).array();
//从16字节中获取整数
字节[]字节=新字节[16];
ByteBuffer缓冲区=ByteBuffer.wrap(字节);

对于(inti=0;i,在java.nio包中使用ByteBuffer

//Convert unsigned short to bytes:
//java has no unsigned short. Char is the equivalent.
char unsignedShort = 100;
//Endianess of bytes. I recommend setting explicitly for clarity
ByteOrder order = ByteOrder.BIG_ENDIAN;
byte[] ary = ByteBuffer.allocate(2).putChar(value).order(order).array();

//get integers from 16 bytes
byte[] bytes = new byte[16];
ByteBuffer buffer= ByteBuffer.wrap(bytes);
for(int i=0;i<4;i++){
    int intValue = (int)buffer.getInt();
}
//将无符号短字符转换为字节:
//java没有无符号的short。Char是等效的。
char unsignedShort=100;
//字节的尾数。为了清晰起见,我建议显式设置
字节顺序=字节顺序.BIG_ENDIAN;
byte[]ary=ByteBuffer.allocate(2).putChar(value).order(order).array();
//从16字节中获取整数
字节[]字节=新字节[16];
ByteBuffer缓冲区=ByteBuffer.wrap(字节);

对于(int i=0;ii),我最终利用了这个资源:


我将这两个字节复制为一个短字节,然后将其转换为一个整数并删除了符号。

我最终利用了这个资源:


我把这两个字节复制成一个短字符,然后把它转换成一个int并删除了符号。

ByteBuffer.wrap(字节)。getInt()
会做同样的事情吗?是的,先生,我会添加一个例子。ByteBuffer.wrap(字节)。getInt()
会做同样的事情吗?是的,先生,我会添加一个例子。