我已经看到了一些类似的问题,但我无法做到这一点:
我在 NSData 对象中收到一个 16 位有符号整数,例如“0xFF3C”。我怎样才能从中读出正确的数字?
编辑:随着詹姆斯韦伯斯特的回答,我得到:
Received hex value : <0800>
Converted value: 803995656
Received hex value : <0a00>
Converted value: 803995658
Received hex value : <1a00>
Converted value: 803995674
Received hex value : <faff>
Converted value: 804061178
Received hex value : <e4ff>
Converted value: 804061156
Received hex value : <f0ff>
Converted value: 804061168
这显然是不正确的(我预计 +/- 2000)
编辑:一些代码:
-(void)characteristicValueRead:(CBCharacteristic *)characteristic{
unsigned short myInt = (int) [characteristic.value bytes];
NSLog(@"Received value: %@", characteristic.value);
NSLog(@"Converted value: %i",(int)myInt);
}
有了这个片段,我得到:
Received value: <0000>
Converted value: 26176
Received value: <0000>
Converted value: 46688
Received value: <0000>
Converted value: 30576
Received value: <0000>
Converted value: 2656
Received value: <0000>
Converted value: 50896
Received value: <0000>
这看起来真的很有趣/烦人。这可能是什么来的?