Java Android 8位到16位表示负数
请参阅问题。 我得到了这样的答案Java Android 8位到16位表示负数,java,android,android-layout,Java,Android,Android Layout,请参阅问题。 我得到了这样的答案 short yourinteger16 = (short)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF)); 方法 protected Integer get8ByteTo16Byte(int firstValue, int secondValue) { Short integerValue = (short)((((byte) firstValue & 0xF
short yourinteger16 = (short)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
方法
protected Integer get8ByteTo16Byte(int firstValue, int secondValue) {
Short integerValue = (short)((((byte) firstValue & 0xFF) << 8) | ((byte) secondValue & 0xFF));
return new Integer(integerValue);
}
受保护整数获取8字节到16字节(int firstValue,int secondValue){
Short integerValue=(Short)((byte)firstValue&0xFF)您完美地接收到了-39和-16(分别是-10000的高字节和低字节)
使用加法
而不是或
对高位字节和低位字节进行加密。
请尝试以下方法
short result = (short) (((short)(-39 & (byte)0xFF) << 8) + (short)(-16 & (byte)0xFF));
short result=(short)((short)(-39&(byte)0xFF)如何创建浮点?生成的短值是什么?Integer integerValue=(((byte)firstValue&0xFF)啊,等一下……那是十六进制-39还是十进制-39?整数到浮点的转换中有没有计算错误?我从BLE设备中得到的值是两个字节,分别是-39和-16是的,现在负值是-10。但是我们如何为正值和负值创建一个通用方法呢?它可能会从BLE动态变化。我们不能预测值。现在正值产生了问题。你的意思是…-10000?我尝试了所有组合:-39和-16产生-10000
39和16产生10000-39和16产生-9968。我没有看到任何例外。哪个组合对你不起作用?是的,你的答案是正确的ect也适用于正值。谢谢,Sahil Bajaj。但是分数呢?这可能会出错,对分数有什么解决方案吗?
protected Integer get8ByteTo16Byte(int firstValue, int secondValue) {
Short integerValue = (short)((((byte) firstValue & 0xFF) << 8) | ((byte) secondValue & 0xFF));
return new Integer(integerValue);
}
short result = (short) (((short)(-39 & (byte)0xFF) << 8) + (short)(-16 & (byte)0xFF));