How to Convert Long to Byte Array In Java

In this tutorial we will learn how to convert long to byte array java . We will write see multiple example for long to byte array java conversion . We will convert long to byte array using java.nio.ByteBuffer, Shift operations and DataOutputStream . We will see also bytebuffer to byte array example .

Example 1 : Convert Long to Byte Array java using ByteBuffer

In this example we convert long to byte array java using java.nio.ByteBuffer class . We will initialize Long then using ByteBuffer class convert to byte array. Let’s see java program for bytebuffer to byte array .

import java.nio.ByteBuffer;
import java.util.Arrays;

public class LongToByteArray {
	
	public static void main(String[] args) {
		Long i  = 17291917291729L;
		byte [] bytes = ByteBuffer.allocate(8).putLong(i).array();
	    System.out.println(Arrays.toString(bytes));
	    
	}

}

Output :

[0, 0, 15, -70, 22, -106, 112, -47]

Example 2: Convert Long to Byte Array java using Shift operations

In this example we will convert long to byte array java using shift operations . We initialize long variable i and then perform shift operations .

import java.util.Arrays;

public class LongToByteArray1 {
	
	public static void main(String[] args) {
		Long i  = 17291917291729L;
		byte[] bytes = longtoBytes(i);
		System.out.println(Arrays.toString(bytes));
	}
	
	
	private static byte[] longtoBytes(long data) {
		 return new byte[]{
		 (byte) ((data >> 56) & 0xff),
		 (byte) ((data >> 48) & 0xff),
		 (byte) ((data >> 40) & 0xff),
		 (byte) ((data >> 32) & 0xff),
		 (byte) ((data >> 24) & 0xff),
		 (byte) ((data >> 16) & 0xff),
		 (byte) ((data >> 8) & 0xff),
		 (byte) ((data >> 0) & 0xff),
		 };
		}
	
}

Output :

[0, 0, 15, -70, 22, -106, 112, -47]

Example 3: Convert Long to Byte Array java using DataOutputStream

In this example we will convert long to byte array java using java.io.ByteArrayOutputStream and java.io.DataOutputStream classes . We will initialize long variable (i) then perform operation for long to byte array in java .Let’s see code for that .

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class LongToByteArray2 {
	public static void main(String[] args) {

		Long i = 17291917291729L;
		byte[] bytes = null;
		try {
			bytes = longToByteArray(i);
			System.out.println(Arrays.toString(bytes));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static byte[] longToByteArray(final long i) throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		dos.writeLong(i);
		dos.flush();
		return bos.toByteArray();
	}

}

Output:

[0, 0, 15, -70, 22, -106, 112, -47]

In this tutorial we learned how to convert long to byte array in java using java.nio.ByteBuffer class , java.io.DataOutputStream class and Shift operations . We have seen bytebuffer to byte array example in java .You can learn more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 3 =