我试图转换一个无符号的有符号字节。问题是我接收的数据是无符号的,Java不支持无符号字节,所以当它读取数据时,它将其视为有符号的。

我尝试通过下面的解决方案转换它,我从Stack Overflow。

public static int unsignedToBytes(byte a)
{
    int b = a & 0xFF;
    return b;
}

但是当它再次以字节为单位转换时,我得到了相同的带符号数据。我试图使用此数据作为参数的Java函数,只接受一个字节作为参数,所以我不能使用任何其他数据类型。我该如何解决这个问题?


当前回答

我认为其他答案已经涵盖了内存表示,您如何处理这些取决于您计划如何使用它的上下文。我要补充的是,Java 8增加了一些处理无符号类型的支持。在这种情况下,您可以使用Byte.toUnsignedInt

int unsignedInt = Byte.toUnsignedInt(myByte);

其他回答

虽然Java语言中没有包含无符号字节看起来很烦人(来自C),但这真的不是什么大问题,因为一个简单的“b & 0xFF”操作在(很少)实际需要的情况下为(有符号)字节b产生无符号值。比特实际上并没有改变——只是解释(这只在例如对值进行一些数学运算时很重要)。

你还可以:

public static int unsignedToBytes(byte a)
{
    return (int) ( ( a << 24) >>> 24);
}    

解释:

假设a = (byte) 133;

在内存中,它被存储为:"1000 0101"(十六进制中的0x85)

所以它的表示可以转换为unsigned=133, signed=-123(作为2的补码)

A << 24

当向左移动24位时,结果现在是一个4字节的整数,表示为:

"10000101 00000000 00000000 00000000"(或十六进制中的"0x85000000")

然后我们有

(a << 24) >>>

它又向右移动了24位,但前导都是0。结果是:

"00000000 00000000 00000000 10000101"(或十六进制中的"0x00000085")

这就是无符号表达式它等于133。

如果你试图强制转换a = (int) a; 然后会发生的是,它保留了字节的2补表示,并将其存储为int,同时也存储为2的补:

(int)“10000101”——>“11111111 11111111 11111111 10000101”

翻译过来就是-123

我试图使用此数据作为参数的Java函数,只接受一个字节作为参数

这与函数接受一个大于2^32-1的整数并没有本质上的区别。

这听起来似乎取决于函数是如何定义和记录的;我认为有三种可能:

It may explicitly document that the function treats the byte as an unsigned value, in which case the function probably should do what you expect but would seem to be implemented wrong. For the integer case, the function would probably declare the parameter as an unsigned integer, but that is not possible for the byte case. It may document that the value for this argument must be greater than (or perhaps equal to) zero, in which case you are misusing the function (passing an out-of-range parameter), expecting it to do more than it was designed to do. With some level of debugging support you might expect the function to throw an exception or fail an assertion. The documentation may say nothing, in which case a negative parameter is, well, a negative parameter and whether that has any meaning depends on what the function does. If this is meaningless then perhaps the function should really be defined/documented as (2). If this is meaningful in an nonobvious manner (e.g. non-negative values are used to index into an array, and negative values are used to index back from the end of the array so -1 means the last element) the documentation should say what it means and I would expect that it isn't what you want it to do anyway.

在Java中没有原始无符号字节。通常的做法是将其转换为更大的类型:

int anUnsignedByte = (int) aSignedByte & 0xff;

如果你认为你正在寻找这样的东西。

public static char toUnsigned(byte b) {
    return (char) (b >= 0 ? b : 256 + b);
}