如何将字节数组转换为其数值(Java)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1026761/
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
How to convert a byte array to its numeric value (Java)?
提问by pirate
I have an 8 byte array and I want to convert it to its corresponding numeric value.
我有一个 8 字节的数组,我想将其转换为相应的数值。
e.g.
例如
byte[] by = new byte[8]; // the byte array is stored in 'by'
// CONVERSION OPERATION
// return the numeric value
I want a method that will perform the above conversion operation.
我想要一种执行上述转换操作的方法。
采纳答案by Mnementh
Assuming the first byte is the least significant byte:
假设第一个字节是最低有效字节:
long value = 0;
for (int i = 0; i < by.length; i++)
{
value += ((long) by[i] & 0xffL) << (8 * i);
}
Is the first byte the most significant, then it is a little bit different:
第一个字节是最重要的,那么它有点不同:
long value = 0;
for (int i = 0; i < by.length; i++)
{
value = (value << 8) + (by[i] & 0xff);
}
Replace long with BigInteger, if you have more than 8 bytes.
如果您有超过 8 个字节,请用BigInteger替换 long 。
Thanks to Aaron Digulla for the correction of my errors.
感谢 Aaron Digulla 纠正我的错误。
回答by coobird
One could use the Buffer
s that are provided as part of the java.nio
package to perform the conversion.
可以使用Buffer
作为java.nio
包的一部分提供的s来执行转换。
Here, the source byte[]
array has a of length 8, which is the size that corresponds with a long
value.
这里,源byte[]
数组的长度为 8,这是与long
值对应的大小。
First, the byte[]
array is wrapped in a ByteBuffer
, and then the ByteBuffer.getLong
method is called to obtain the long
value:
首先将byte[]
数组包裹在 a 中ByteBuffer
,然后ByteBuffer.getLong
调用该方法获取long
值:
ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 4});
long l = bb.getLong();
System.out.println(l);
Result
结果
4
I'd like to thank dfa for pointing out the ByteBuffer.getLong
method in the comments.
我要感谢 dfaByteBuffer.getLong
在评论中指出了方法。
Although it may not be applicable in this situation, the beauty of the Buffer
s come with looking at an array with multiple values.
尽管在这种情况下它可能不适用,但Buffer
s的美妙之处在于查看具有多个值的数组。
For example, if we had a 8 byte array, and we wanted to view it as two int
values, we could wrap the byte[]
array in an ByteBuffer
, which is viewed as a IntBuffer
and obtain the values by IntBuffer.get
:
例如,如果我们有一个 8 字节的数组,并且希望将其视为两个int
值,则可以将byte[]
数组包装在 an 中ByteBuffer
,将其视为 aIntBuffer
并通过IntBuffer.get
以下方式获取值:
ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 1, 0, 0, 0, 4});
IntBuffer ib = bb.asIntBuffer();
int i0 = ib.get(0);
int i1 = ib.get(1);
System.out.println(i0);
System.out.println(i1);
Result:
结果:
1
4
回答by Vincent Robert
If this is an 8-bytes numeric value, you can try:
如果这是一个 8 字节的数值,您可以尝试:
BigInteger n = new BigInteger(byteArray);
If this is an UTF-8 character buffer, then you can try:
如果这是一个 UTF-8 字符缓冲区,那么您可以尝试:
BigInteger n = new BigInteger(new String(byteArray, "UTF-8"));
回答by iBog
Complete java converter code for all primitive types to/from arrays http://www.daniweb.com/code/snippet216874.html
所有原始类型到/从数组的完整 java 转换器代码 http://www.daniweb.com/code/snippet216874.html
回答by Chen Yi
Simply, you could use or refer to guavalib provided by google, which offers utiliy methods for conversion between long and byte array. My client code:
简单来说,你可以使用或参考谷歌提供的guavalib,它提供了用于长数组和字节数组之间转换的实用方法。我的客户代码:
long content = 212000607777l;
byte[] numberByte = Longs.toByteArray(content);
logger.info(Longs.fromByteArray(numberByte));
回答by Jamel Toms
You can also use BigInteger for variable length bytes. You can convert it to Long, Integer or Short, whichever suits your needs.
您还可以将 BigInteger 用于可变长度字节。您可以根据需要将其转换为 Long、Integer 或 Short。
new BigInteger(bytes).intValue();
or to denote polarity:
或表示极性:
new BigInteger(1, bytes).intValue();
回答by Asaf Pinhassi
Each cell in the array is treated as unsigned int:
数组中的每个单元格都被视为无符号整数:
private int unsignedIntFromByteArray(byte[] bytes) {
int res = 0;
if (bytes == null)
return res;
for (int i=0;i<bytes.length;i++){
res = res | ((bytes[i] & 0xff) << i*8);
}
return res;
}