Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert byte Array to Hex String in Java
In Java, Converting a byte array to a hex string means transforming each byte into its hexadecimal representation.
- byte array: A byte array is a collection of byte values. Each byte is an 8-bit unit of data.
- Hex String: A hexadecimal (hex) string represents a number in base-16, using digits 0-9 and letters A-F. We represent the A-F in numbers as 10 to 15.
The following is our byte array as given below ?
byte[] b = new byte[]{'p', 'q', 'r'};
We have created a custom method "display" that takes a byte array as input. This method converts the byte array to a hex string.
public static String display(byte[] b1) {
StringBuilder strBuilder = new StringBuilder();
for(byte val : b1) {
strBuilder.append(String.format("%02x", val&0xff));
}
return strBuilder.toString();
}
To Convert byte Array to Hex String in Java is quite easy. Let's learn the following methods:
Converting byteArray to hex string using StringBuilder Class
To convert a byteArray to a hex string We have a StringBuilder class in Java. This class belongs to the java.lang package, used to create and manipulate a string can be modified.
Each byte in the array is converted to a two-digit hexadecimal string and appended to a 'StringBuilder'. The resulting hex string is then returned, with trailing spaces removed.
Example
In this example, we convert a byte array containing ASCII values into a string ("pqr"). Then, it uses a 'StringBuilder' to convert the same byte array into a hexadecimal string representation ("70 71 72").
public class Main {
public static void main(String args[]) {
byte[] b = new byte[]{'p', 'q', 'r'};
/* byte array cannot be displayed as String because it may have non-printable
characters e.g. 0 is NUL, 5 is ENQ in ASCII format */
String str = new String(b );
System.out.println(str);
// byte array to Hex String
System.out.println("Byte array to Hex String = " + display(b));
}
public static String display(byte[] b1) {
StringBuilder strBuilder = new StringBuilder();
for (byte val : b1) {
strBuilder.append(String.format("%02x ", val & 0xff));
}
return strBuilder.toString().trim(); // Remove trailing space
}
}
Output
The above program produce the following result ?
pqr Byte array to Hex String = 70 71 72
Converting byteArray to hex string using BigInteger Class
To convert a byteArray into a hex string in Java, we have a BigInteger class that belongs to Java.math package. This package is used to perform mathematical operations on large integers outside the range of the primitive int and long types.
We create a BigInteger object from the byte array and then convert it to a hexadecimal string representation using bigInt.toString(16). This converts the byte array into a hex string format.
Example
This is another example of converting a byte array to a hex string using the BigInteger class. It initializes a BigInteger with the byte array and then converts it to a hex string using the toString(16) method.
import java.math.BigInteger;
public class Main {
public static void main(String args[]) {
byte[] b = new byte[]{'p', 'q', 'r'};
/* byte array cannot be displayed as String because it may have non-printable
characters e.g. 0 is NUL, 5 is ENQ in ASCII format */
String str = new String(b);
System.out.println(str);
System.out.println("Byte array to Hex String = " + display(b));
}
public static String display(byte[] b1) {
BigInteger bigInt = new BigInteger(1, b1);
return bigInt.toString(16);
}
}
Output
The above program produce the following result ?
pqr Byte array to Hex String = 707172