简体   繁体   中英

how do i convert byte array to long in android

I need to convert a byte[] of size 4 to type long for my Android app. I found a function byteArrayToLong(byte_array) which is in the java.io.Bits package, but it isn't working in my application.

Are there any other alternative functions?

Use java.nio.ByteBuffer :

 byte[] data = new byte[] {50, -106, 40, -22}; ByteBuffer buffer = ByteBuffer.wrap(data); System.out.println(buffer.getLong()); 

Edit

Of course, @Kaito is right: you need an array of 8 bytes instead. Also, as others mentioned, you may choose an order in which your bytes will be read ( BIG_ENDIAN by default):

    byte[] data = new byte[] {50, -106, 40, -22, 0, 0, 0, 0};
    ByteBuffer buffer = ByteBuffer.wrap(data);
    buffer.order(ByteOrder.BIG_ENDIAN);
    System.out.println(buffer.getLong()); // 3645145933890453504
    buffer = ByteBuffer.wrap(data);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    System.out.println(buffer.getLong()); // 3928528434

Also, why to prefer an API method? For the sake of simplicity: instead of recognizing byte to long conversion in bitwise shifts you can read what happens here in plain English.

THE DOCTOR's answer works, but I'd do it sans the loop.

For little-endian (the most likely case):

long val = b[0] | ((int)(b[1]) << 8) | ((int)(b[2]) << 16) | ((int)(b[3]) << 24);

For big-endian:

long val = b[3] | ((int)(b[2]) << 8) | ((int)(b[1]) << 16) | ((int)(b[0]) << 24);

Also, keep in mind that Java long is 8 bytes long. For 4 bytes, an int is sufficient.

The answer depends on whether your first byte is the least or the most significant byte.

If 1st byte is most significant :

long val = 0;

for (int i = 0; i < YourByte.length; i++)
{
   val = (val << 8) + (YourByte[i] & 0xff);
}

If 1st byte is the least significant byte :

long val = 0;

for (int i = 0; i < YourByte.length; i++)
{
   val += ((long) YourByte[i] & 0xffL) << (i*8);
}

Edit :

Use BigInteger instead of long when dealing with more than 8 bytes.

Three options come to mind:

  1. Pass the array to the BigInteger constructor then fetch the long value from the BigInteger
  2. Pad the array with another 4 bytes and use ByteBuffer.wrap then getLong
  3. Construct it manually by shifting each byte into place.

Depending on the endianness you may have to reverse the array.

When converting a byte[ ] of size less than 8 bytes to type long

public static ByteBuffer byteArraytoByteBuffer(int capacity,byte[] array) { ByteBuffer buffer = ByteBuffer.allocate(capacity).put(array).order(ByteOrder.LITTLE_ENDIAN); buffer.rewind(); return buffer; }

then

byte[] input = new byte[]{1,0,0,0};
long value = byteArraytoByteBuffer(8,input).getLong();

1. ByteBuffer.allocate(capacity) allocates 8 bytes for type long.

buffer = {0,0,0,0,0,0,0,0}

2. put(array) will place the 4 bytes

buffer = {1,0,0,0,0,0,0,0}

           ^
           |

now the position of buffer is 4

3.Byte order cab be LITTLE_ENDIAN or BIG_ENDIAN based on requirement.

4. buffer.rewind() Rewinds the buffer and position is set to zero,

buffer = {1,0,0,0,0,0,0,0}

       ^
       |

if buffer.rewind() operation is not done an buffer underflow will occur.Because the position is at 4, when getLong() is called it takes start index as 4.

The same code can be used to convert byte[ ] to int

byte[] input = new byte[]{1,0,0,0};
long value = byteArraytoByteBuffer(4,input).getInt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM