In this article, we will see how to convert a java long value into a byte array.
We may need this in many scenarios. An example would be…
In a cluster of machines, one member may want to send another member a heartbeat message containing its alive time in which case you need to convert the system time into a byte array and then send them over the wire.
Convert long to bytes
In java, long is a signed 64-bit type, which means it contains 8 bytes.
Assuming the long value is 1436808134035
, Let’s start converting long value to bytes.
In the form of bits, grouped by bytes, the long value would be:
00000000 00000000 00000001 01001110 10001000 01110000 00111101 10010011
We will create an array of 8 bytes. We want each group to be assigned to one byte array element.
- We typecast long to the last element of byte array, the lowest 8 bits will get assigned to the byte.
b[7] = (byte) (L)
b[7]
would get 10010011 - Now we want the next 8 bits of long to get assigned to one more element of byte array. We achieve this using unsigned right shift and reassigning the value to long.
L >>>= 8;
. We do unsigned right shift so that the signed bit is shifted too, replacing it with a zero bit. The long value after the unsigned right shift would be:
00000000 00000000 00000000 00000001 01001110 10001000 01110000 00111101
. - We again follow the first step, that is, to typecast it to the next lower element of the byte array.
b[6] = (byte) (L);
. This will preserve the lower 8 bits of the new long value. After typecast the
b[6]
will get00111101
- Again, we do the unsigned right shift to preserve the next 8 bits of long
- We will continue the steps from 1-2 till we reach the last 8 bits of long
LongToByteArrayExample:
package com.javarticles.io; import java.io.IOException; public class LongToByteArrayExample { public static void main(String[] args) throws IOException { long aliveTime = System.currentTimeMillis(); System.out.println("Time: " + aliveTime); } private static void longToByteArray(long l, byte[] b) { b[7] = (byte) (l); l >>>= 8; b[6] = (byte) (l); l >>>= 8; b[5] = (byte) (l); l >>>= 8; b[4] = (byte) (l); l >>>= 8; b[3] = (byte) (l); l >>>= 8; b[2] = (byte) (l); l >>>= 8; b[1] = (byte) (l); l >>>= 8; b[0] = (byte) (l); } }
Output:
Time: 1436808134035
Convert bytes to long
We just need to do the reverse to convert the byte array back to long.
- Remember, the lower 8 bits of long are stored in the 8th element of the byte array.
- Using
AND
operation with the 8th element of byte array and0xFF
, we get the lower 8 bits of the long value. - Again, to get the next 8 bits of long follow step 2 on the next lower element of the byte array
- Left shift the value by 8 bits and add it to the previous long value
- Keep repeating the steps till you reach the final first element of the byte array
LongToByteArrayExample:
package com.javarticles.io; import java.io.IOException; public class LongToByteArrayExample { public static void main(String[] args) throws IOException { long aliveTime = System.currentTimeMillis(); System.out.println("Time: " + aliveTime); byte[] longAsBytes = new byte[8]; longToByteArray(aliveTime, longAsBytes); long retrievedAliveTime = byteArrayToLong(longAsBytes); System.out.println("Retrieved Time: " + retrievedAliveTime); } private static void longToByteArray(long l, byte[] b) { b[7] = (byte) (l); l >>>= 8; b[6] = (byte) (l); l >>>= 8; b[5] = (byte) (l); l >>>= 8; b[4] = (byte) (l); l >>>= 8; b[3] = (byte) (l); l >>>= 8; b[2] = (byte) (l); l >>>= 8; b[1] = (byte) (l); l >>>= 8; b[0] = (byte) (l); } private static long byteArrayToLong(byte[] b){ return ( ( (long) b[7]) & 0xFF) + ( ( ( (long) b[6]) & 0xFF) << 8) + ( ( ( (long) b[5]) & 0xFF) << 16) + ( ( ( (long) b[4]) & 0xFF) << 24) + ( ( ( (long) b[3]) & 0xFF) << 32) + ( ( ( (long) b[2]) & 0xFF) << 40) + ( ( ( (long) b[1]) & 0xFF) << 48) + ( ( ( (long) b[0]) & 0xFF) << 56); } }
Output:
Time: 1436808134035 Retrieved Time: 1436808134035
Download the source code
This was an example about converting java Long into Byte array and then convert the byte array back to long.