【发布时间】:2019-05-12 11:41:50
【问题描述】:
我有两个字节的数据。我将它们中的每一个都转换为 Uint8,然后我从它们中生成了一个 Uint16。
如何生成这个 Uint16 数字的二进制补码?
我尝试过uInt16 = ~uInt16 + 1,但代码会生成 32 位整数,我希望它保持为 16 位整数。
byte firstByte, secondByte;
int firstUint8, secondUint8, uInt16;
firstByte = buffer[index];//get first byte from buffer
secondByte = buffer[index + 1];//get second byte from buffer
firstUint8=firstByte & 0xFF;//produce Uint8
secondUint8 = secondByte & 0xFF;//produce Uint8
uInt16 = 256 * firstUint8 + secondUint8;//create Uint16 from these to Uint8
twosComplementOfUInt16=~number+1; //produce 32 bit integer but I want int16
【问题讨论】:
-
在 Java 中,
int是 32 位的。你到底期待什么? -
int是 32 位的。你应该使用short。 -
简洁明了的措辞。