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
// 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] + " ");
}
}
}
OutputHex 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] + " ");
}
}
OutputHexadecimal 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]+" ");
}
}
}
OutputHex String : 1f3d44
Byte Array : 31 61 68
Similar Reads
Java Program to Convert Byte Array to Hex 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. 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 hexadec
5 min read
Java Program to Convert Byte Array to Image
A byte array is the array of bytes that is used to store the collection of binary data. For example, the byte array of an image stores the information of every pixel of the image. In the byte array, we can store the content of any file in binary format. We can initialize the byte array with the byte
3 min read
Java Program to Convert File to a Byte Array
Here, we will go through the different ways to convert file to byte array in Java. Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs. Methods: Using rea
3 min read
Java Program to Convert Byte Array to Long
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 an array of bytes, convert it to a long value. Example: Byte Array: 1 2 3 4 Long Value: 16909060 Equivalent Hexadecimal String: 0x1020304 There are numerous approaches fo
3 min read
Java Program to Convert String to Byte Array Using getBytes() Method
In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset an
2 min read
Java Program to Convert Byte Array to Object
Converting byte array into Object and Object into a byte array process is known as deserializing and serializing. The class object which gets serialized/deserialized must implement the interface Serializable. Serializable is a marker interface that comes under package 'java.io.Serializable'.Byte Arr
2 min read
Java Program to Convert String to Long
To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a
3 min read
Java Program to Convert String to InputStream
Given a string, the task is to convert the string to InputStream which is shown in the below illustrations. Illustration: Input : String : "Geeks for Geeks" Output : Input Stream : Geeks for Geeks Input : String : "A computer science portal" Output : Input stream : A computer science portal In order
2 min read
Java Program to Convert InputStream to String
Read and Write operations are basic functionalities that users perform in any application. Every programming language provides I/O streams to read and write data. The FileInputStream class and FileOutputStream class of Java performs I/O operations on files. The FileInputStream class is used to read
4 min read
Java Program to Convert Char to Byte
Given a char in Java, the task is to write a Java program that converts this char into Byte. Examples: Input: ch = 'A' Output: 65 Input: ch = 'B' Output 66 In Java, char is a primitive data type and it is used to declare characters. It has the capability to hold 16-bit unsigned Unicode characters. T
3 min read