Home » Java HexFormat – byte arrays and hexadecimal conversion

Java HexFormat – byte arrays and hexadecimal conversion

  • by
HexFormat byte arrays hexadecimal

1. Overview

In this article, we will learn to convert between byte arrays and hexadecimal string using the HexFormat class introduced in Java SE \ JDK 17. For conversion between primitive types such as byte, char, int, long and hexadecimal string, refer to this article.

2. Convert between byte arrays and hexadecimal using Java HexFormat

The HexFormat is a dedicated class for converting between byte arrays and hexadecimal strings.

Consider you have an array of bytes that has to be converted into a Hexadecimal string. You can easily do it by using this HexFormat utility class. This is available in standard library java.util package.

To start with, you must retrieve the instance of HexFormat class using any of the following factory methods:

  1. of() – No delimiter. However, you can update delimiter later using helper method withDelimiter(String delimiter).
  2. ofDelimiter(String delimiter) – Specified delimiter.

You can use a delimiter to separate the hexadecimal values in the formatted hexadecimal string. Both of the above hexadecimal formatters return the hexadecimal values in lowercase letters (a to f). However, you can change this behavior by using the utility method withUpperCase().

3. Java HexFormat – convert from Byte array to hexadecimal string

The HexFormat contains formatHex methods to handle the byte array formatting to hexadecimal.

3.1. Hexadecimal string with no delimiter

As mentioned earlier, you can use of or ofDelimiter factory methods to get the HexFormat instance. If you use of factory method, then no delimiter is set by default.

At first, formatHex takes each byte from the array and converts it to hexadecimal values. Then, it appends each hexadecimal value together as a hexadecimal string.

In the below example, we have an array of bytes. The formatHex converts each byte in the array to a hexadecimal value, such as byte 126 converted to 7E, 127 to 7F, so on. Finally, it appends all the hexadecimal values as a single string 000102037C7D7E7F.

HexFormat hex = HexFormat.of().withUpperCase();
byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
String str = hex.formatHex(bytes);

System.out.println("Byte Array to Hexadecimal formatted string: " + str);

/* prints Byte Array to Hexadecimal formatted string: 000102037C7D7E7F */

3.2. Byte array to hexadecimal with delimiter

To separate the hexadecimal values using a delimiter in the formatted hexadecimal string, then you must first configure your HexFormat instance:

1. If you already have a HexFormat instance, then invoke withDelimiter instance method.

HexFormat hex = HexFormat.of().withUpperCase();
hex = hex.withDelimiter(".");

2. To create a new HexFormat instance with a delimiter, then use ofDelimiter factory method.

HexFormat hexFormat = HexFormat.ofDelimiter(".");

Now, we have configured our Hexadecimal formatted. Let’s invoke the formatHex method and watch the results.

HexFormat hexFormat = HexFormat.ofDelimiter(".");
byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
String str = hexFormat.formatHex(bytes);

System.out.println("Byte Array to Hexadecimal formatted string: " + str);

/* prints Byte Array to Hexadecimal formatted string: 00.01.02.03.7C.7D.7E.7F */

When we run the above code, it prints the hexadecimal string with the values separated by the delimiter (.) dot.

3.3. Hexadecimal string formatted with prefix and suffix

Alternatively, you can add a prefix or suffix to the hexadecimal string. To do so, you must first configure your HexFormat instance.

1. Use withPrefix to configure the prefix

hexFormat = hexFormat.withPrefix("#");

2. Use withSuffix to configure the suffix

hexFormat = hexFormat.withSuffix("$");

After configuration, you can run the below code to observe the results.

HexFormat hexFormat = HexFormat.ofDelimiter(".");
hexFormat = hexFormat.withPrefix("#");
hexFormat = hexFormat.withSuffix("%");

byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
String str = hexFormat.formatHex(bytes);

System.out.println("Byte Array to Hexadecimal formatted string: " + str);
Byte Array to Hexadecimal formatted string: #00%.#01%.#02%.#03%.#7c%.#7d%.#7e%.#7f%

4. Hexadecimal to Byte Array conversion using HexFormat

You can convert the hexadecimal formatted string back to a byte array using parseHex method. Let’s take the same example which we have discussed in the previous section.

We have created a hexadecimal formatted string str from bytes array using the formatHex method. Later, we converted the str back to bytes using parseHex.

HexFormat hexFormat = HexFormat.ofDelimiter(".");
hexFormat = hexFormat.withPrefix("#");
hexFormat = hexFormat.withSuffix("%");

byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
String str = hexFormat.formatHex(bytes);

System.out.println("Byte Array to Hexadecimal formatted string: " + str);
/* prints Byte Array to Hexadecimal formatted string: #00%.#01%.#02%.#03%.#7c%.#7d%.#7e%.#7f% */

byte[] parsed = hexFormat.parseHex(str);
System.out.println("Arrays.equals(bytes, parse):" +Arrays.equals(bytes, parsed));

/* prints Arrays.equals(bytes, parse):true */

5. Conclusion

To sum up, we have learned to convert between byte array and hexadecimal formatted string using the HexFormat class.

Leave a Reply

Your email address will not be published. Required fields are marked *