如何获得14位,2';s将补码值转换为整数或短值,并在Java中维护符号
我有一个14位的补码值,以字节数组的形式从蓝牙设备发送到java应用程序 我无法更改从蓝牙设备发送的数据的格式 如何将该值转换为整数或短值并保持符号 我尝试过的一些事情是如何获得14位,2';s将补码值转换为整数或短值,并在Java中维护符号,java,byte,bytearray,Java,Byte,Bytearray,我有一个14位的补码值,以字节数组的形式从蓝牙设备发送到java应用程序 我无法更改从蓝牙设备发送的数据的格式 如何将该值转换为整数或短值并保持符号 我尝试过的一些事情是 Short x = ByteBuffer.wrap(value).getShort(); Integer x = (short)(value[3] & 0xff) | (short)((value[2] << 8) & 0xff00); Short x=ByteBuffer.wrap(value
Short x = ByteBuffer.wrap(value).getShort();
Integer x = (short)(value[3] & 0xff) | (short)((value[2] << 8) & 0xff00);
Short x=ByteBuffer.wrap(value.getShort();
整数x=(短)(值[3]&0xff)|(短)((值[2]应将最高有效位向左扩展
即兴:short y=(short)((x&0b100000000)>0?(x&0b11000000000000):x);
也就是说,如果最高有效位(对于14位数字)是1,则&的结果将是非零。如果是,则在左侧添加两个附加的位,并加上or。否则保持原样。您应该将最高有效位向左扩展
即兴:short y=(short)((x&0b100000000)>0?(x&0b11000000000000):x);
也就是说,如果最高有效位(对于14位数字)如果是1,&的结果将是非零。如果是这样,请在左边加上两个附加值,并加上or。否则保持原样。我已经生成了一个使用整数的小示例,应该很容易将您检索到的缩写放在整数中。为了确保这一点,我已经展示了一个使用值
变量的示例
/**
* Expects the value to contain a 2-complement number stored in the
* least significant bits.
* The contents of the most significant bits is ignored.
* @param value the value to convert
* @param bitSize the size of the 2 complement number in the value
* @return the converted value
*/
public static int fromTwoComplement(int value, int bitSize) {
int shift = Integer.SIZE - bitSize;
// shift sign into position
int result = value << shift;
// Java right shift uses sign extension, but only works on integers or longs
result = result >> shift;
return result;
}
public static void main(String[] args) {
System.out.println(fromTwoComplement(0b00_00_0000_0000_0001, 14));
System.out.println(fromTwoComplement(0b00_01_1111_1111_1111, 14));
System.out.println(fromTwoComplement(0b00_10_0000_0000_0000, 14));
System.out.println(fromTwoComplement(0b00_11_1111_1111_1111, 14));
// largest negative
byte[] value = new byte[] { 0b0010_0000, 0b0000_0000 };
System.out.println(fromTwoComplement((int) ByteBuffer.wrap(value).getShort(), 14));
}
/**
*期望该值包含存储在
*最低有效位。
*最高有效位的内容将被忽略。
*@param value要转换的值
*@param bitSize值中2个补码的大小
*@返回转换后的值
*/
来自两个补码的公共静态int(int值,int位大小){
int shift=Integer.SIZE—位大小;
//将标志移动到位
int结果=值>移位;
返回结果;
}
公共静态void main(字符串[]args){
System.out.println(来自两个补码(0b00_00_0000_0000_0001,14));
System.out.println(来自两个补码(0b00_01_1111_1111,14));
System.out.println(来自两个补码(0b00_10_0000_0000_0000,14));
System.out.println(来自两个补码(0b00_11_1111_1111,14));
//最大负数
字节[]值=新字节[]{0b0010_0000,0b0000_0000};
System.out.println(fromtowcomplete((int)ByteBuffer.wrap(value.getShort(),14));
}
我已经生成了一个使用整数的小示例,应该很容易将检索到的短字符放入整数中。为了确保这一点,我已经展示了一个使用值
变量的示例
/**
* Expects the value to contain a 2-complement number stored in the
* least significant bits.
* The contents of the most significant bits is ignored.
* @param value the value to convert
* @param bitSize the size of the 2 complement number in the value
* @return the converted value
*/
public static int fromTwoComplement(int value, int bitSize) {
int shift = Integer.SIZE - bitSize;
// shift sign into position
int result = value << shift;
// Java right shift uses sign extension, but only works on integers or longs
result = result >> shift;
return result;
}
public static void main(String[] args) {
System.out.println(fromTwoComplement(0b00_00_0000_0000_0001, 14));
System.out.println(fromTwoComplement(0b00_01_1111_1111_1111, 14));
System.out.println(fromTwoComplement(0b00_10_0000_0000_0000, 14));
System.out.println(fromTwoComplement(0b00_11_1111_1111_1111, 14));
// largest negative
byte[] value = new byte[] { 0b0010_0000, 0b0000_0000 };
System.out.println(fromTwoComplement((int) ByteBuffer.wrap(value).getShort(), 14));
}
/**
*期望该值包含存储在
*最低有效位。
*最高有效位的内容将被忽略。
*@param value要转换的值
*@param bitSize值中2个补码的大小
*@返回转换后的值
*/
来自两个补码的公共静态int(int值,int位大小){
int shift=Integer.SIZE—位大小;
//将标志移动到位
int结果=值>移位;
返回结果;
}
公共静态void main(字符串[]args){
System.out.println(来自两个补码(0b00_00_0000_0000_0001,14));
System.out.println(来自两个补码(0b00_01_1111_1111,14));
System.out.println(来自两个补码(0b00_10_0000_0000_0000,14));
System.out.println(来自两个补码(0b00_11_1111_1111,14));
//最大负数
字节[]值=新字节[]{0b0010_0000,0b0000_0000};
System.out.println(fromtowcomplete((int)ByteBuffer.wrap(value.getShort(),14));
}
只需从字节数组中提取数字,完全忽略符号和多余位:
byte[] data = ...
int rawNum = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
byte[]数据=。。。
intrawnum=(数据[0]和0xFF)(32-14);
这将首先通过向左移位将符号位移动到存储值的类型的符号位中(此处:int),然后在向右移位时复制符号位移位距离倍(由移位运算符定义)。只需从字节数组中提取数字,完全忽略符号和多余位:
byte[] data = ...
int rawNum = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
byte[]数据=。。。
intrawnum=(数据[0]和0xFF)(32-14);
这将首先通过向左移位将符号位移动到存储值的类型的符号位(此处:int),然后在向右移位时复制符号位移位距离次数(由移位运算符定义).在二进制数据源中,您只应该读取14位还是14位有符号整数被存储为短整数?当一个数字为负数时,所有位的位置都设置为1,当该数字变为负数时,1被0取代(基本上与二进制计数时的情况完全相反)它是以字节数组的形式接收的,但我想我应该只读取14位。在二进制源中,您应该只读取14位,还是14位有符号整数被存储为短整数?当一个数字变为负数时,所有位的位置都设置为1,当该数字变大为负数时,1被0取代(基本上与二进制计数完全相反)它作为字节数组接收,但我想我应该只读取14位。我以前没有见过这种声明字节的方法(0b1000000000000000)它应该工作还是只是简写符号?当我在编辑器中尝试它时,它会注册一个编译错误。另外,如果我以字节数组的形式接收数据,我想我只是使用类似这样的方法来转换为int
-整数rawX=(short)(value[3]&0xff)|(short)((value[2]在发布之前,我使用Eclipse Luna和JDK 7对此进行了测试。您的Java版本可能低于7。请参阅。请注意,自2013年2月以来,Java 6一直没有公开更新,这是由于字节文字方面的改进。我实际上在安卓Studio中使用Java 7,出于某种原因,它仍然显示为错误,但事实并非如此