Skip to content
Related Articles

Related Articles

Java Program to Convert Hex String to Byte Array

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 Sep, 2021
Improve Article
Save Article

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




// Java Program to convert hex
// string to byte array
 
// Approach 1 - Using parseInt() method of
// Integer class in Java
 
import java.io.*;
 
public class GFG {
   public static void main(String[] args)
   {
      
     // Initializing the hex string and byte array
     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;
        
           // Using parseInt() method of Integer class
         int val = Integer.parseInt(s.substring(index, index + 2), 16);
         ans[i] = (byte)val;
     }
         
     // Printing the required Byte Array
     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




// Java Program to convert hex
// string to byte array
 
// Approach 1 - Using Byte Array Representation
// of BigInteger in Java
 
import java.io.*;
// importing BigInteger class
import java.math.BigInteger;
 
class GFG {
    public static void main(String[] args)
    {
        String s = "3b4a11";
           
          // converting string to integer value
        int val = Integer.parseInt(s, 16);
        System.out.println("Hexadecimal String : " + s);
           
          // converting integer value to Byte Array
        BigInteger big = BigInteger.valueOf(val);
        byte[] ans = (big.toByteArray());
       
          // printing the byte array
        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




// Java program to convert hex
// string to byte array
 
// Approach 3 - Using Bitwise Shift Operators
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
           
         // initializing hex string and byte array
        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) {
             // using left shift operator on every character
            ans[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
           
          // printing the required result
          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 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!