Java Program to Convert Hex String to Byte Array
Last Updated :
24 Sep, 2021
Hex String – A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0’s and 1’s. Eg: “245FC” is a hexadecimal string.
Byte Array – A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0.
Given a byte array, the task is to convert the Hex String to Byte Array.
Example:
Input - Hex String : "2f4a33"
Output - Byte Array : 47 74 51
Input - Hex String : "3b4a11"
Output - Byte Array : 59 74 17
There are numerous approaches for converting a Hex String to Byte Array, and a few of them are listed below.
Approaches:
- Using parseInt() method of Integer class in Java
- Using Byte Array Representation of BigInteger in Java
- Using Bitwise Shift Operators
Approach 1 – Using parseInt() method of Integer class in Java
The integer.parseInt() method in Java converts a given string to an integer. According to the given problem statement, since we have to convert a hex string to a byte array, we will first convert the characters of the hex string in pairs to the specific byte formation and insert that number into the byte array. In this, the byte array is initialized with a size of half the length of the hex string.
Following is the implementation of the foregoing approach –
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
String s = "2f4a33" ;
byte [] ans = new byte [s.length() / 2 ];
System.out.println( "Hex String : " +s);
for ( int i = 0 ; i < ans.length; i++) {
int index = i * 2 ;
int val = Integer.parseInt(s.substring(index, index + 2 ), 16 );
ans[i] = ( byte )val;
}
System.out.print( "Byte Array : " );
for ( int i = 0 ; i < ans.length; i++) {
System.out.print(ans[i] + " " );
}
}
}
|
Output
Hex String : 2f4a33
Byte Array : 47 74 51
Approach 2 – Using Byte Array Representation of BigInteger in Java
In this approach, we will use the toByteArray() method of BigInteger class. After converting the hexadecimal number to an integer value using the parseInt() method, we are left to convert integers to a byte array. Here comes the role of toByteArray() method of BigInteger class which will transform the integer values to a byte array and return it.
Following is the implementation of the foregoing approach –
Java
import java.io.*;
import java.math.BigInteger;
class GFG {
public static void main(String[] args)
{
String s = "3b4a11" ;
int val = Integer.parseInt(s, 16 );
System.out.println( "Hexadecimal String : " + s);
BigInteger big = BigInteger.valueOf(val);
byte [] ans = (big.toByteArray());
System.out.print( "Byte Array : " );
for ( int i = 0 ; i < ans.length; i++)
System.out.print(ans[i] + " " );
}
}
|
Output
Hexadecimal String : 3b4a11
Byte Array : 59 74 17
Approach 3 – Using Bitwise Shift Operators
Another way to convert a hex string to a byte array is to use the Binary shift operators of Java. Here “<<” bitwise left shift operator is used. In order to get the numeric value of the character in hexadecimal order, the Character.digit() method in Java is used.
Following is the implementation of the foregoing approach –
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
String s = "1f3d44" ;
System.out.println( "Hex String : " + s);
int len = s.length();
byte [] ans = new byte [len / 2 ];
for ( int i = 0 ; i < len; i += 2 ) {
ans[i / 2 ] = ( byte ) ((Character.digit(s.charAt(i), 16 ) << 4 )
+ Character.digit(s.charAt(i+ 1 ), 16 ));
}
System.out.print( "Byte Array : " );
for ( int i= 0 ;i<ans.length;i++){
System.out.print(ans[i]+ " " );
}
}
}
|
Output
Hex String : 1f3d44
Byte Array : 31 61 68
Share your thoughts in the comments
Please Login to comment...