Reputation: 85
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?
Upvotes: 3
Views: 7108
Reputation: 1
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();
Upvotes: 0
Reputation: 3338
Use java.nio.ByteBuffer
:
byte[] data = new byte[] {50, -106, 40, -22};
ByteBuffer buffer = ByteBuffer.wrap(data);
System.out.println(buffer.getLong());
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.
Upvotes: 2
Reputation: 61408
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.
Upvotes: 2
Reputation: 4555
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.
Upvotes: 2
Reputation: 1765
Three options come to mind:
Depending on the endianness you may have to reverse the array.
Upvotes: 1