Java Reference ยท Text, Formatting and Regex
The HexFormat class
Reference for java.util.HexFormat (Java 17): formatHex, parseHex, delimiters, prefixes, uppercase output and single-value conversions for bytes, ints and longs.
java.util.HexFormat (since Java 17) converts between bytes and hexadecimal text. Before it, Java had no standard way to turn a byte[] into a hex string; code used String.format("%02x", b) in loops, BigInteger tricks or third-party libraries. HexFormat does it in one call and also parses the text back.
An instance is immutable and holds the formatting options: a delimiter between bytes, a prefix and suffix for each byte, and lower or upper case digits. Start from HexFormat.of() and derive customized instances with withDelimiter, withPrefix, withSuffix and withUpperCase.
HexFormat hex = HexFormat.of(); String s = hex.formatHex(new byte[] {10, (byte) 255}); // "0aff" byte[] b = hex.parseHex("0aff");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class HexFormat static HexFormat of() static HexFormat ofDelimiter(String delimiter)
| Method | Description |
|---|---|
formatHex(byte[]), formatHex(byte[], int from, int to) |
Bytes to a hex String. Also overloads that append to an Appendable. |
parseHex(CharSequence) |
Hex text to byte[], honoring the configured delimiter, prefix and suffix. |
withDelimiter(String), withPrefix(String), withSuffix(String) |
Return a new format with that option. |
withUpperCase(), withLowerCase() |
Select digit case for output. |
toHexDigits(byte / char / short / int / long) |
Fixed-width hex text for one value; toHexDigits(long, int digits) for a chosen length. |
fromHexDigits(CharSequence), fromHexDigitsToLong(CharSequence) |
Parse an unprefixed hex number to int / long. |
toHighHexDigit(int), toLowHexDigit(int) |
The two hex characters of a byte. |
static isHexDigit(int), static fromHexDigit(int) |
Test and convert one digit. |
Throws: IllegalArgumentException when parsed text has an odd number of digits, a non-hex character or does not fit the delimiter/prefix/suffix; NumberFormatException (a subclass) from fromHexDigits; IndexOutOfBoundsException for bad ranges.
Usage notes
- Fixed width.
toHexDigits(int)always emits eight characters. UseInteger.toHexStringwhen you want no leading zeros. - No negative sign. Bytes, ints and longs are printed as their two's-complement bit patterns (
(byte) -1isff). - Digit case only affects output. Parsing accepts both
AFandaf. - Delimiter and parsing.
parseHexexpects the delimiter between bytes exactly as configured; otherwise it fails. - Replaces loops over
String.format("%02x"), which are slow for large arrays, andnew BigInteger(1, bytes).toString(16), which drops leading zeros. - Thread-safe and immutable, so a shared
static final HexFormatis fine. - Typical uses: printing digests (
MessageDigest.digest()), parsing MAC addresses and byte dumps, and buildingUUID-like strings. - Not available before Java 17; on older versions use
Integer.toHexStringorString.format.