解决所有 Java 字节都已签名这一事实的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11088/
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
What is the best way to work around the fact that ALL Java bytes are signed?
提问by Max
In Java, there is no such thing as an unsigned byte.
在 Java 中,没有无符号字节这样的东西。
Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign.
使用一些低级代码时,有时您需要使用无符号值大于 128 的字节,这会导致 Java 将它们解释为负数,因为 MSB 用于符号。
What's a good way to work around this? (Saying don't use Java is not an option)
解决这个问题的好方法是什么?(说不使用 Java 不是一种选择)
采纳答案by pauldoo
When reading any single value from the array copy it into something like a short or an int and manually convert the negative number into the positive value it should be.
当从数组中读取任何单个值时,将其复制为 short 或 int 之类的内容,然后手动将负数转换为它应该是的正值。
byte[] foobar = ..;
int value = foobar[10];
if (value < 0) value += 256 // Patch up the 'falsely' negative value
You can do a similar conversion when writing into the array.
您可以在写入数组时进行类似的转换。
回答by Nick Berardi
I guess you could just use a short to store them. Not very efficient, but really the only option besides some herculean effort that I have seen.
我想你可以用一个短路来存储它们。效率不高,但确实是除了我所见过的一些艰巨努力之外的唯一选择。
回答by stimms
Probably your best bet is to use an integer rather than a byte. It has the room to allow for numbers greater than 128 without the overhead of having to create a special object to replace byte.
可能你最好的选择是使用整数而不是字节。它有空间允许大于 128 的数字,而无需创建特殊对象来替换字节的开销。
This is also suggested by people smarter than me (everybody)
这也是比我聪明的人建议的(每个人)
回答by martinatime
The best way to do bit manipulation/unsigned bytes is through using ints. Even though they are signed they have plenty of spare bits (32 total) to treat as an unsigned byte. Also, all of the mathematical operators will convert smaller fixed precision numbers to int. Example:
进行位操作/无符号字节的最佳方法是使用ints。即使它们是有符号的,它们也有大量的备用位(总共 32 个)作为无符号字节处理。此外,所有数学运算符都会将较小的固定精度数字转换为int。例子:
short a = 1s;
short b = 2s;
int c = a + b; // the result is up-converted
short small = (short)c; // must cast to get it back to short
Because of this it is best to just stick with integer and mask it to get the bits that you are interested in. Example:
因此,最好只使用整数并屏蔽它以获得您感兴趣的位。例如:
int a = 32;
int b = 128;
int foo = (a + b) | 255;
Here is some more info on Java primitive types http://mindprod.com/jgloss/primitive.html
这是有关 Java 原始类型的更多信息http://mindprod.com/jgloss/primitive.html
One last trivial note, there is one unsigned fixed precision number in Java. That is the charprimitive.
最后一点,Java 中有一个无符号的固定精度数。那就是char原语。
回答by izb
Using ints is generally better than using shorts because java uses 32-bit values internally anyway (Even for bytes, unless in an array) so using ints will avoid unnecessary conversion to/from short values in the bytecode.
使用 ints 通常比使用 shorts 更好,因为 java 在内部使用 32 位值(即使是字节,除非在数组中),因此使用 ints 将避免在字节码中与短值进行不必要的转换。
回答by eHyman
It is actually possible to get rid of the if statement and the addition if you do it like this.
如果您这样做,实际上可以摆脱 if 语句和添加。
byte[] foobar = ..;
int value = (foobar[10] & 0xff);
This way Java doesn't interpret the byte as a negative number and flip the sign bit on the integer also.
这样 Java 不会将字节解释为负数,也不会翻转整数上的符号位。
回答by octo
I know this is a very late response, but I came across this thread when trying to do the exact same thing. The issue is simply trying to determine if a Java byte is >127.
我知道这是一个很晚的回应,但我在尝试做完全相同的事情时遇到了这个线程。问题只是试图确定 Java 字节是否大于 127。
The simple solution is:
简单的解决方法是:
if((val & (byte)0x80) != 0) { ... }
If the real issue is >128 instead, just adding another condition to that if-statement will do the trick.
如果真正的问题是 >128,只需向该 if 语句添加另一个条件即可解决问题。