Byte to Hex in Java
Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0.
Hex String - Just as a binary string only consists of 0s and 1s, a hex strings is made up of the numerals 0 through 9 and the letters A through F. Like the hexadecimal string "245FC."
Example:
The aim is to translate a byte array into a hex string given a byte array.
1. byteArray = 9, 2, 14, 10 as an input
Results: 9 2 E A
2. byteArray = 7, 12, 13, and 127 as an input
Results: 7 C D 7F
A byte datatype array is converted towards its hexadecimal value as a string when it is converted from a Byte Array into Hex String. There are several ways to accomplish the same thing; some of them are described here.
Approaches:
- Using Java's Format() Method
- Bitwise shift operators are used
- Using the Integer/Long Class's default method
- Java's BigInteger Representation in Hexadecimal
Method 1: Using Java's Format()
The requested conversion can be performed using Java's String Format() function. For this,
Calculate the hexadecimal equivalent for every byte in the array by iterating through the bytes in the array.
When printing a hexadecimal value's number of places and storing the result in a string, the string.format() function is utilized.
A hexadecimal (X) value is printed with two spaces between adjacent hexadecimal values using the formatting %02X.
Example Program
import java.io.*;
public class Demo {
public static void convertByteToHex(byte[] byteArray)
{
String hex = "";
for (byte i :byteArray) {
hex += String.format("%02X", i);
}
System.out.print(hex);
}
public static void main(String[] args)
{
byte[] byteArray = { 3, 13, 12, 16 };
convertByteToHex(byteArray);
}
}
Output
030D0C10
Method 2: Utilizing Bitwise Shift Operators
When using the prior method, the process becomes cumbersome as the byte array grows larger. To improve performance, the byte array is transformed into a hexadecimal value using a byte operation.
The right shift operator with no sign is used in this case, ">>>." Additionally, the toCharArray() method turns the supplied string into a string of characters.
Example program
import java.io.*;
public class Demo {
public static void
convertByteToHex(byte[] byteArray)
{
int len = byteArray.length;
char[] hexValues = "0123456789ABCDEF".toCharArray();
char[] hexCharacter = new char[len * 2];
for (int i = 0; i<len; i++) {
int v = byteArray[i] & 0xFF;
hexCharacter[i * 2] = hexValues[v >>> 4];
hexCharacter[i * 2 + 1] = hexValues[v & 0x0F];
}
System.out.println(hexCharacter);
}
public static void main(String[] args)
{
byte[] bytes = { 8, 3, 12, 14 };
convertByteToHex(bytes);
}
}
Output
08030C0E
Method 3: Using the Integer/Long Class predefined method
An integer can be converted to its hexadecimal counterpart using the toHexString() function of the Integer class. Now, we must utilize this method to convert every byte array into such an integer (for 4-sized arrays) or long (for 8-sized arrays). The wrap function of a ByteBuffer class can be used to convert a byte array to an integer or long.
Example Program
import java.io.*;
import java.nio.*;
public class Demo {
public static String toHexadecimal(byte[] bytes)
{
StringBuilder result = new StringBuilder();
for (byte i : bytes) {
int decimal = (int)i& 0XFF;
String hex = Integer.toHexString(decimal);
if (hex.length() % 2 == 1) {
hex = "0" + hex;
}
result.append(hex);
}
return result.toString();
}
public static void main(String[] args)
{
byte[] byteArray = { 8, 3, 13, 12 };
System.out.println(toHexadecimal(byteArray));
}
}
Output
08030d0c
Method 4: Java BigInteger Representation in Hexadecimal
Due to its sluggish speed, Java's Hexadecimal Version of BigInteger class is often avoided when converting byte arrays to hex strings. Furthermore, since we are dealing with numerals and not just any old byte string, leading zeros may occasionally be omitted.
Example Program
import java.io.*;
import java.math.BigInteger;
public class Demo {
public static void toHexString(byte[] byteArray)
{
System.out.print(
new BigInteger(1, byteArray).toString(16));
}
public static void main(String[] args)
{
byte[] byteArray = { 18, 3, 15, 12 };
toHexString(byteArray);
}
}
Output
12030f0c