如何将字节数组转换为其数值(Java)?

时间:2021-05-17 21:24:17

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.

我想要一个方法来执行上面的转换操作。

7 个解决方案

#1


94  

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更正我的错误。

#2


101  

One could use the Buffers that are provided as part of the java.nio package to perform the conversion.

可以使用作为java一部分提供的缓冲区。nio包执行转换。

Here, the source byte[] array has a of length 8, which is the size that corresponds with a long value.

在这里,源字节[]数组的长度为8,即与长值对应的大小。

First, the byte[] array is wrapped in a ByteBuffer, and then the ByteBuffer.getLong method is called to obtain the long value:

首先,字节[]数组封装在ByteBuffer中,然后是ByteBuffer。调用getLong方法获取长值:

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.

我要感谢dfa指出了ByteBuffer。getLong方法在注释中。


Although it may not be applicable in this situation, the beauty of the Buffers come with looking at an array with multiple values.

尽管在这种情况下可能不适用,但是缓冲区的优点是可以看到具有多个值的数组。

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值,我们可以将字节[]数组封装在一个ByteBuffer中,这个ByteBuffer被看作是一个IntBuffer,通过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

#3


15  

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"));

#4


13  

Simply, you could use or refer to guava lib provided by google, which offers utiliy methods for conversion between long and byte array. My client code:

简单地说,您可以使用或引用谷歌提供的guava lib,它提供用于长数组和字节数组之间转换的实用方法。我的客户端代码:

    long content = 212000607777l;
    byte[] numberByte = Longs.toByteArray(content);
    logger.info(Longs.fromByteArray(numberByte));

#5


7  

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();

#6


3  

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。

#7


2  

Each cell in the array is treated as unsigned int:

数组中的每个单元格都被视为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;
}

#1


94  

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更正我的错误。

#2


101  

One could use the Buffers that are provided as part of the java.nio package to perform the conversion.

可以使用作为java一部分提供的缓冲区。nio包执行转换。

Here, the source byte[] array has a of length 8, which is the size that corresponds with a long value.

在这里,源字节[]数组的长度为8,即与长值对应的大小。

First, the byte[] array is wrapped in a ByteBuffer, and then the ByteBuffer.getLong method is called to obtain the long value:

首先,字节[]数组封装在ByteBuffer中,然后是ByteBuffer。调用getLong方法获取长值:

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.

我要感谢dfa指出了ByteBuffer。getLong方法在注释中。


Although it may not be applicable in this situation, the beauty of the Buffers come with looking at an array with multiple values.

尽管在这种情况下可能不适用,但是缓冲区的优点是可以看到具有多个值的数组。

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值,我们可以将字节[]数组封装在一个ByteBuffer中,这个ByteBuffer被看作是一个IntBuffer,通过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

#3


15  

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"));

#4


13  

Simply, you could use or refer to guava lib provided by google, which offers utiliy methods for conversion between long and byte array. My client code:

简单地说,您可以使用或引用谷歌提供的guava lib,它提供用于长数组和字节数组之间转换的实用方法。我的客户端代码:

    long content = 212000607777l;
    byte[] numberByte = Longs.toByteArray(content);
    logger.info(Longs.fromByteArray(numberByte));

#5


7  

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();

#6


3  

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。

#7


2  

Each cell in the array is treated as unsigned int:

数组中的每个单元格都被视为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;
}