Posts

Showing posts with the label int

Java Program to Convert byte[] to int and int to byte[]

In this section, we will show you h ow to convert byte[] to int and int to byte[] using ByteBuffer. int to  byte[] int num = 1 ; // int need 4 bytes, default ByteOrder.BIG_ENDIAN byte [] result = ByteBuffer . allocate ( 4 ).putInt( num ).array(); byte[]  to  int   byte [] bytes = new byte [] { 0 , 00 , 00 , 01 }; int num = ByteBuffer . wrap ( bytes ).getInt(); 1. int to byte[] This Java example converts an  int   into a byte array and prints it in hex format. public class Main { public static void main ( String [] args) { int num = 1 ; byte [] result = convertIntToByteArray ( num ); System . out .println( "Input : " + num ); System . out .println( "Byte Array (Hex) : " + convertBytesToHex ( result )); } // method 1, int need 4 bytes, // default ByteOrder.BIG_ENDIAN public static byte [] convertIntToByteArray ( int value) { return ByteBuffer . allocate ( 4 )