Utilities: HexFormat converts bytes to and from a hex string

HexFormat.of() gives a reusable formatter that turns a byte array into a lowercase hex string with formatHex, and parses one back into bytes with parseHex, replacing the hand-rolled loops developers used to write.

Code
byte[] data = {0x0A, (byte) 0xFF, 0x42};
HexFormat hex = HexFormat.of();
String encoded = hex.formatHex(data);
System.out.println("Encoded: " + encoded);
byte[] decoded = hex.parseHex(encoded);
System.out.println("Round trip equal: " + Arrays.equals(data, decoded));
Output
Encoded: 0aff42
Round trip equal: true
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms