Java:将字节转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4492711/
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
Java: Convert byte to integer
提问by keshav
I need to convert 2 byte array ( byte[2] ) to integer value in java. How can I do that?
我需要在 java 中将 2 字节数组( byte[2] )转换为整数值。我怎样才能做到这一点?
回答by Matt Ball
You can use ByteBuffer
for this:
您可以ByteBuffer
为此使用:
ByteBuffer buffer = ByteBuffer.wrap(myArray);
buffer.order(ByteOrder.LITTLE_ENDIAN); // if you want little-endian
int result = buffer.getShort();
See also Convert 4 bytes to int.
另请参阅将 4 个字节转换为 int。
回答by Chris Dodd
Well, each byte is an integer in the range -128..127, so you need a way to map a pair of integers to a single integer. There are many ways of doing that, depending on what you have encoded in the pair of bytes. The most common will be storing a 16-bit signed integer as a pair of bytes. Converting that back to an integer depends on whether you store it big-endian form:
嗯,每个字节都是 -128..127 范围内的整数,因此您需要一种方法将一对整数映射到单个整数。有很多方法可以做到这一点,具体取决于您在这对字节中编码的内容。最常见的是将 16 位有符号整数存储为一对字节。将其转换回整数取决于您是否以大端格式存储它:
(byte_array[0]<<8) + (byte_array[1] & 0xff)
or little endian:
或小端:
(byte_array[1]<<8) + (byte_array[0] & 0xff)
回答by DiveInto
In Java, Bytes are signed, which means a byte's value can be negative, and when that happens, @MattBall's original solution won't work.
在 Java 中,字节是有符号的,这意味着字节的值可能为负,当发生这种情况时,@MattBall 的原始解决方案将不起作用。
For example, if the binary form of the bytes array are like this:
例如,如果字节数组的二进制形式是这样的:
1000 1101 1000 1101
1000 1101 1000 1101
then myArray[0] is 1000 1101
and myArray[1] is 1000 1101
, the decimal value of byte 1000 1101
is -115
instead of 141(= 2^7 + 2^3 + 2^2 + 2^0)
然后myArray的[0]是1000 1101
与myArray的[1]是1000 1101
,字节的十进制值1000 1101
是-115
代替的141(= 2 ^ 7 + 2 ^ 3 + 2 ^ 2 + 2 ^ 0)
if we use
如果我们使用
int result = (myArray[0] << 8) + myArray[1]
int result = (myArray[0] << 8) + myArray[1]
the value would be -16191
which is WRONG.
值将-16191
是错误的。
The reason why its wrong is that when we interpret a 2-byte array into integer, all the bytes are unsigned, so when translating, we should map the signed bytes to unsigned integer:
错误的原因是,当我们将一个2字节的数组解释为整数时,所有字节都是无符号的,因此在翻译时,我们应该将有符号字节映射到无符号整数:
((myArray[0] & 0xff) << 8) + (myArray[1] & 0xff)
((myArray[0] & 0xff) << 8) + (myArray[1] & 0xff)
the result is 36237
, use a calculator or ByteBuffer to check if its correct(I have done it, and yes, it's correct).
结果是36237
,使用计算器或 ByteBuffer 检查它是否正确(我已经完成了,是的,它是正确的)。
回答by Nacho Coloma
Also, if you can use the Guava library:
另外,如果您可以使用 Guava 库:
Ints.fromByteArray(0, 0, myArray[1], myArray[0]);
It was worth mentioning since a lot of projects use it anyway.
值得一提的是,无论如何,很多项目都在使用它。
回答by Droid Chris
Simply do this:
只需这样做:
return new BigInteger(byte[] yourByteArray).intValue();
Works great on Bluetooth command conversions etc. No need to worry about signed vs. unsigned conversion.
在蓝牙命令转换等方面效果很好。无需担心有符号与无符号转换。
回答by MAHESH BABU.M
import java.io.*;
public class ByteArray {
public static void main(String[] args) throws IOException {
File f=new File("c:/users/sample.txt");
byte[]b={1,2,3,4,5};
ByteArrayInputStream is=new ByteArrayInputStream(b);
int i;
while((i=is.read())!=-1) {
System.out.println((int)i);
FileOutputStream f1=new FileOutputStream(f);
FileOutputStream f2=new FileOutputStream(f);
ByteArrayOutputStream b1=new ByteArrayOutputStream();
b1.write(6545);
b1.writeTo(f1);
b1.writeTo(f2);
b1.close();
}