java 从 2 或 4 个字节转换为有符号/无符号短/整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10803557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert from 2 or 4 bytes to signed/unsigned short/int
提问by blackwolf
I have to convert bytes to signed/unsigned int or short.
我必须将字节转换为有符号/无符号整数或短整数。
The methods below are correct? Which is signed and which unsigned?
下面的方法对吗?哪个是签名的,哪个是未签名的?
Byte order: LITTLE_ENDIAN
字节顺序:LITTLE_ENDIAN
public static int convertTwoBytesToInt1(byte b1, byte b2) {
return (int) ((b2 << 8) | (b1 & 0xFF));
}
VS.
对比。
public static int convertTwoBytesToInt2(byte b1, byte b2) {
return (int) (( (b2 & 0xFF) << 8) | (b1 & 0xFF));
}
and
和
public static int convertFourBytesToInt1(byte b1, byte b2, byte b3, byte b4){
return (int) ((b4<<24)+(b3<<16)+(b2<<8)+b1);
}
VS.
对比。
public static int convertFourBytesToInt2(byte b1, byte b2, byte b3, byte b4){
return (int) (( (b4 & 0xFF) << 24) | ((b3 & 0xFF) << 16) | ((b2 & 0xFF) << 8) | (b1 & 0xFF));
}
I'm interested onlyin this conversion forms. Thanks!
我只对这种转换形式感兴趣。谢谢!
回答by erickson
The first method (convertXXXToInt1()
) of each pair is signed, the second (convertXXXToInt2()
) is unsigned.
convertXXXToInt1()
每对的第一个方法 ( ) 是有符号的,第二个 ( convertXXXToInt2()
) 是无符号的。
However, Java int
is always signed, so if the highest bit of b4
is set, the result of convertFourBytesToInt2()
will be negative, even though this is supposed to be the "unsigned" version.
但是,Javaint
始终是有符号的,因此如果设置了最高位b4
,则结果convertFourBytesToInt2()
将为负,即使这应该是“无符号”版本。
Suppose a byte
value, b2
is -1, or 0xFF in hexadecimal. The <<
operator will cause it to be "promoted" to an int
type with a value of -1, or 0xFFFFFFFF. After the shift of 8 bits, it will be 0xFFFFFF00, and after a shift of 24 bytes, it will be 0xFF000000.
假设一个byte
值为b2
-1 或 0xFF 的十六进制值。所述<<
操作者将导致其被“提升”到一个int
为-1,或0xFFFFFFFF值类型。移位8位后为0xFFFFFF00,移位24个字节后为0xFF000000。
However, if you apply the bitwise &
operator, the higher-order bits will be set to zero. This discards the sign information. Here are the first steps of the two cases, worked out in more detail.
但是,如果应用按位运算&
符,高位将设置为零。这会丢弃标志信息。以下是这两个案例的第一步,更详细地制定了。
Signed:
签:
byte b2 = -1; // 0xFF
int i2 = b2; // 0xFFFFFFFF
int n = i2 << 8; // 0x0xFFFFFF00
Unsigned:
未签名:
byte b2 = -1; // 0xFF
int i2 = b2 & 0xFF; // 0x000000FF
int n = i2 << 8; // 0x0000FF00
回答by dmolony
There is a problem with the 4-byte unsigned conversion, because it doesn't fit into an int. The routines below work correctly.
4 字节无符号转换存在问题,因为它不适合 int。下面的例程正常工作。
public class IntegerConversion
{
public static int convertTwoBytesToInt1 (byte b1, byte b2) // signed
{
return (b2 << 8) | (b1 & 0xFF);
}
public static int convertFourBytesToInt1 (byte b1, byte b2, byte b3, byte b4)
{
return (b4 << 24) | (b3 & 0xFF) << 16 | (b2 & 0xFF) << 8 | (b1 & 0xFF);
}
public static int convertTwoBytesToInt2 (byte b1, byte b2) // unsigned
{
return (b2 & 0xFF) << 8 | (b1 & 0xFF);
}
public static long convertFourBytesToInt2 (byte b1, byte b2, byte b3, byte b4)
{
return (long) (b4 & 0xFF) << 24 | (b3 & 0xFF) << 16 | (b2 & 0xFF) << 8 | (b1 & 0xFF);
}
public static void main (String[] args)
{
byte b1 = (byte) 0xFF;
byte b2 = (byte) 0xFF;
byte b3 = (byte) 0xFF;
byte b4 = (byte) 0xFF;
System.out.printf ("%,14d%n", convertTwoBytesToInt1 (b1, b2));
System.out.printf ("%,14d%n", convertTwoBytesToInt2 (b1, b2));
System.out.printf ("%,14d%n", convertFourBytesToInt1 (b1, b2, b3, b4));
System.out.printf ("%,14d%n", convertFourBytesToInt2 (b1, b2, b3, b4));
}
}