Java : HexFormat with Examples

HexFormat (Java SE 18 & JDK 18) API Examples.
You will find code examples on most HexFormat methods.


Summary

HexFormat converts between bytes and chars and hex-encoded strings which may include additional formatting markup such as prefixes, suffixes, and delimiters.

Class diagram

final var hex = HexFormat.of();

final byte value1 = 127;
final var ret1 = hex.toHexDigits(value1);
System.out.println(ret1); // 7f

final short value2 = 0x1ab;
final var ret2 = hex.toHexDigits(value2);
System.out.println(ret2); // 01ab

final int value3 = -1;
final var ret3 = hex.toHexDigits(value3);
System.out.println(ret3); // ffffffff
final byte[] bytes = {10, 11, 12, 13, 14, 15, 16};

final var hex = HexFormat.ofDelimiter(", ");
final var ret1 = hex.formatHex(bytes);
System.out.println(ret1); // 0a, 0b, 0c, 0d, 0e, 0f, 10

final var prefixHex = hex.withPrefix("0x");
final var ret2 = prefixHex.formatHex(bytes);
System.out.println(ret2); // 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10

Methods

String delimiter ()

Returns the delimiter between hexadecimal values in formatted hexadecimal strings.

final byte[] bytes = {1, 2, 3, 10, 11, 12};

final var hex1 = HexFormat.of();
System.out.println(hex1.delimiter().isEmpty()); // true
System.out.println(hex1.formatHex(bytes)); // 0102030a0b0c

final var hex2 = HexFormat.ofDelimiter("-");
System.out.println(hex2.delimiter()); // -
System.out.println(hex2.formatHex(bytes)); // 01-02-03-0a-0b-0c

boolean equals (Object o)

Returns true if the other object is a HexFormat with the same parameters.

final var hex1 = HexFormat.of();
final var hex2 = HexFormat.ofDelimiter("-");

System.out.println(hex1.equals(hex2)); // false

final var hex3 = hex1.withDelimiter("-");

System.out.println(hex1.equals(hex3)); // false
System.out.println(hex2.equals(hex3)); // true

String formatHex (byte[] bytes)

Returns a hexadecimal string formatted from a byte array.

final byte[] bytes = {10, 11, 12, 13, 14, 15, 16};

final var hex = HexFormat.ofDelimiter(", ");
final var ret = hex.formatHex(bytes);
System.out.println(ret); // 0a, 0b, 0c, 0d, 0e, 0f, 10

final var prefixHex = hex.withPrefix("0x");
final var ret2 = prefixHex.formatHex(bytes);
System.out.println(ret2); // 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10

String formatHex (byte[] bytes, int fromIndex, int toIndex)

Returns a hexadecimal string formatted from a byte array range.

Please see also : formatHex(byte[] bytes)

final byte[] bytes = {1, 2, 3, 4};
final var hex = HexFormat.ofDelimiter(", ").withPrefix("0x");

System.out.println(hex.formatHex(bytes, 0, 0).isEmpty()); // true
System.out.println(hex.formatHex(bytes, 0, 1)); // 0x01
System.out.println(hex.formatHex(bytes, 0, 2)); // 0x01, 0x02
System.out.println(hex.formatHex(bytes, 0, 3)); // 0x01, 0x02, 0x03

System.out.println(hex.formatHex(bytes, 0, 3)); // 0x01, 0x02, 0x03
System.out.println(hex.formatHex(bytes, 1, 3)); // 0x02, 0x03
System.out.println(hex.formatHex(bytes, 2, 3)); // 0x03
System.out.println(hex.formatHex(bytes, 3, 3).isEmpty()); // true

<A extends Appendable> A formatHex (A out, byte[] bytes)

Appends formatted hexadecimal strings from a byte array to the Appendable.

final var hex = HexFormat.ofDelimiter(", ");
final var sb = new StringBuilder();

final byte[] bytes1 = {1, 2, 3};
final var ret1 = hex.formatHex(sb, bytes1);
System.out.println(ret1); // 01, 02, 03

sb.append(" - ");

final byte[] bytes2 = {10, 11, 12};
final var ret2 = hex.formatHex(sb, bytes2);
System.out.println(ret2); // 01, 02, 03 - 0a, 0b, 0c

System.out.println(sb); // 01, 02, 03 - 0a, 0b, 0c

<A extends Appendable> A formatHex (A out, byte[] bytes, int fromIndex, int toIndex)

Appends formatted hexadecimal strings from a byte array range to the Appendable.

Please see also : formatHex(A out, byte[] bytes)

final byte[] bytes = {1, 2, 3, 4};
final var hex = HexFormat.ofDelimiter(", ").withPrefix("0x");

System.out.println(hex.formatHex(new StringBuilder(), bytes, 0, 0).isEmpty()); // true
System.out.println(hex.formatHex(new StringBuilder(), bytes, 0, 1)); // 0x01
System.out.println(hex.formatHex(new StringBuilder(), bytes, 0, 2)); // 0x01, 0x02
System.out.println(hex.formatHex(new StringBuilder(), bytes, 0, 3)); // 0x01, 0x02, 0x03

System.out.println(hex.formatHex(new StringBuilder(), bytes, 0, 3)); // 0x01, 0x02, 0x03
System.out.println(hex.formatHex(new StringBuilder(), bytes, 1, 3)); // 0x02, 0x03
System.out.println(hex.formatHex(new StringBuilder(), bytes, 2, 3)); // 0x03
System.out.println(hex.formatHex(new StringBuilder(), bytes, 3, 3).isEmpty()); // true

static int fromHexDigit (int ch)

Returns the value for the hexadecimal character or codepoint.

final var ret1 = HexFormat.fromHexDigit('0');
System.out.println(ret1); // 0

final var ret2 = HexFormat.fromHexDigit('A');
System.out.println(ret2); // 10

final var ret3 = HexFormat.fromHexDigit('f');
System.out.println(ret3); // 15

try {
    final var ret4 = HexFormat.fromHexDigit('G');
} catch (NumberFormatException e) {
    System.out.println(e);

    // Result
    // ↓
    //java.lang.NumberFormatException: not a hexadecimal digit: "G" = 71
}

static int fromHexDigits (CharSequence string)

Returns the int value parsed from a string of up to eight hexadecimal characters.

final var ret1 = HexFormat.fromHexDigits("10");
System.out.println(ret1); // 16

final var ret2 = HexFormat.fromHexDigits("ffff");
System.out.println(ret2); // 65535

final var ret3 = HexFormat.fromHexDigits("123ABC");
System.out.println(ret3); // 1194684

final var ret4 = HexFormat.fromHexDigits("ffffffff");
System.out.println(ret4); // -1

try {
    final var ret5 = HexFormat.fromHexDigits("123456789");
} catch (IllegalArgumentException e) {
    System.out.println(e);

    // Result
    // ↓
    //java.lang.IllegalArgumentException: string length greater than 8: 9
}

static int fromHexDigits (CharSequence string, int fromIndex, int toIndex)

Returns the int value parsed from a string range of up to eight hexadecimal characters.

final var str = "1234ffff";

System.out.println(str.substring(0, 2)); // 12
System.out.println(HexFormat.fromHexDigits(str, 0, 2)); // 18

System.out.println(str.substring(0, 4)); // 1234
System.out.println(HexFormat.fromHexDigits(str, 0, 4)); // 4660

System.out.println(str.substring(4, 6)); // ff
System.out.println(HexFormat.fromHexDigits(str, 4, 6)); // 255

System.out.println(str.substring(4, 8)); // ffff
System.out.println(HexFormat.fromHexDigits(str, 4, 8)); // 65535

static long fromHexDigitsToLong (CharSequence string)

Returns the long value parsed from a string of up to sixteen hexadecimal characters.

This method is equivalent except a type to fromHexDigits(CharSequence string).

static long fromHexDigitsToLong (CharSequence string, int fromIndex, int toIndex)

Returns the long value parsed from a string range of up to sixteen hexadecimal characters.

This method is equivalent except a type to fromHexDigits(CharSequence string, int fromIndex, int toIndex).

int hashCode ()

Returns a hashcode for this HexFormat.

final var ret1 = HexFormat.of().hashCode();
System.out.println(ret1); // 924758

final var ret2 = HexFormat.ofDelimiter(",").hashCode();
System.out.println(ret2); // 2235562

final var ret3 = HexFormat.of().withPrefix("abc").hashCode();
System.out.println(ret3); // 93520952

static boolean isHexDigit (int ch)

Returns true if the character is a valid hexadecimal character or codepoint.

System.out.println(HexFormat.isHexDigit('0')); // true
System.out.println(HexFormat.isHexDigit('1')); // true
System.out.println(HexFormat.isHexDigit('2')); // true

System.out.println(HexFormat.isHexDigit('a')); // true
System.out.println(HexFormat.isHexDigit('b')); // true
System.out.println(HexFormat.isHexDigit('c')); // true

System.out.println(HexFormat.isHexDigit('D')); // true
System.out.println(HexFormat.isHexDigit('E')); // true
System.out.println(HexFormat.isHexDigit('F')); // true

System.out.println(HexFormat.isHexDigit('g')); // false
System.out.println(HexFormat.isHexDigit('+')); // false
System.out.println(HexFormat.isHexDigit('-')); // false

boolean isUpperCase ()

Returns true if the hexadecimal digits are uppercase, otherwise false.

final var hex = HexFormat.of();

System.out.println(hex.isUpperCase()); // false
System.out.println(hex.toHexDigits(0x123abcd)); // 0123abcd

final var upperCaseHex = hex.withUpperCase();

System.out.println(upperCaseHex.isUpperCase()); // true
System.out.println(upperCaseHex.toHexDigits(0x123abcd)); // 0123ABCD

static HexFormat of ()

Returns a hexadecimal formatter with no delimiter and lowercase characters.

final var hex = HexFormat.of();

// uppercase: false, delimiter: "", prefix: "", suffix: ""
System.out.println(hex);

final int value = 0x123abcd;
System.out.println(hex.toHexDigits(value)); // 0123abcd

final byte[] bytes = {1, 2, 3, 4, 5};
System.out.println(hex.formatHex(bytes)); // 0102030405

static HexFormat ofDelimiter (String delimiter)

Returns a hexadecimal formatter with the delimiter and lowercase characters.

final var hex = HexFormat.ofDelimiter("-");

// uppercase: false, delimiter: "-", prefix: "", suffix: ""
System.out.println(hex);

final int value = 0x123abcd;
System.out.println(hex.toHexDigits(value)); // 0123abcd

final byte[] bytes = {1, 2, 3, 4, 5};
System.out.println(hex.formatHex(bytes)); // 01-02-03-04-05

byte[] parseHex (char[] chars, int fromIndex, int toIndex)

Returns a byte array containing hexadecimal values parsed from a range of the character array.

Please see also : parseHex(CharSequence string)

final var hex = HexFormat.of();
final char[] chars = {'0', '1', '0', '2', '0', '3'};

final var ret1 = hex.parseHex(chars, 0, 2);
System.out.println(Arrays.toString(ret1)); // [1]

final var ret2 = hex.parseHex(chars, 0, 4);
System.out.println(Arrays.toString(ret2)); // [1, 2]

final var ret3 = hex.parseHex(chars, 0, 6);
System.out.println(Arrays.toString(ret3)); // [1, 2, 3]

final var ret4 = hex.parseHex(chars, 2, 6);
System.out.println(Arrays.toString(ret4)); // [2, 3]

final var ret5 = hex.parseHex(chars, 4, 6);
System.out.println(Arrays.toString(ret5)); // [3]

byte[] parseHex (CharSequence string)

Returns a byte array containing hexadecimal values parsed from the string.

final var hex = HexFormat.of();

final var ret1 = hex.parseHex("010203");
System.out.println(Arrays.toString(ret1)); // [1, 2, 3]

final var ret2 = hex.parseHex("0a0b0c");
System.out.println(Arrays.toString(ret2)); // [10, 11, 12]

final var ret3 = hex.parseHex("101112");
System.out.println(Arrays.toString(ret3)); // [16, 17, 18]

final var ret4 = hex.parseHex("fffefd");
System.out.println(Arrays.toString(ret4)); // [-1, -2, -3]
final var hex1 = HexFormat.ofDelimiter(",");

final var ret1 = hex1.parseHex("01,02,03");
System.out.println(Arrays.toString(ret1)); // [1, 2, 3]

final var hex2 = hex1.withPrefix("0x");

final var ret2 = hex2.parseHex("0x0a,0x0b,0x0c");
System.out.println(Arrays.toString(ret2)); // [10, 11, 12]

byte[] parseHex (CharSequence string, int fromIndex, int toIndex)

Returns a byte array containing hexadecimal values parsed from a range of the string.

Please see also : parseHex(CharSequence string)

final var hex = HexFormat.of();
final var str = "010203";

final var ret1 = hex.parseHex(str, 0, 2);
System.out.println(Arrays.toString(ret1)); // [1]

final var ret2 = hex.parseHex(str, 0, 4);
System.out.println(Arrays.toString(ret2)); // [1, 2]

final var ret3 = hex.parseHex(str, 0, 6);
System.out.println(Arrays.toString(ret3)); // [1, 2, 3]

final var ret4 = hex.parseHex(str, 2, 6);
System.out.println(Arrays.toString(ret4)); // [2, 3]

final var ret5 = hex.parseHex(str, 4, 6);
System.out.println(Arrays.toString(ret5)); // [3]

String prefix ()

Returns the prefix used for each hexadecimal value in formatted hexadecimal strings.

final byte[] bytes = {1, 2, 3, 10, 11, 12};

final var hex = HexFormat.ofDelimiter(", ");
System.out.println(hex.prefix().isEmpty()); // true

System.out.println(hex.formatHex(bytes)); // 01, 02, 03, 0a, 0b, 0c

final var prefixHex = hex.withPrefix("0x");
System.out.println(prefixHex.prefix()); // 0x

System.out.println(prefixHex.formatHex(bytes)); // 0x01, 0x02, 0x03, 0x0a, 0x0b, 0x0c

String suffix ()

Returns the suffix used for each hexadecimal value in formatted hexadecimal strings.

final byte[] bytes = {1, 2, 3, 10, 11, 12};

final var hex = HexFormat.ofDelimiter(", ");
System.out.println(hex.suffix().isEmpty()); // true

System.out.println(hex.formatHex(bytes)); // 01, 02, 03, 0a, 0b, 0c

final var suffixHex = hex.withSuffix("KB");
System.out.println(suffixHex.suffix()); // KB

System.out.println(suffixHex.formatHex(bytes)); // 01KB, 02KB, 03KB, 0aKB, 0bKB, 0cKB

String toHexDigits (byte value)

Returns the two hexadecimal characters for the byte value.

final var hex = HexFormat.of();

final byte value1 = 1;
System.out.println(hex.toHexDigits(value1)); // 01

final byte value2 = 10;
System.out.println(hex.toHexDigits(value2)); // 0a

final byte value3 = 16;
System.out.println(hex.toHexDigits(value3)); // 10

final byte value4 = 127;
System.out.println(hex.toHexDigits(value4)); // 7f

final byte value5 = -1;
System.out.println(hex.toHexDigits(value5)); // ff

String toHexDigits (char value)

Returns the four hexadecimal characters for the char value.

final var hex = HexFormat.of();

final char value1 = 1;
System.out.println(hex.toHexDigits(value1)); // 0001

final char value2 = 10;
System.out.println(hex.toHexDigits(value2)); // 000a

final char value3 = 16;
System.out.println(hex.toHexDigits(value3)); // 0010

final char value4 = 127;
System.out.println(hex.toHexDigits(value4)); // 007f

final char value5 = 0x12ab;
System.out.println(hex.toHexDigits(value5)); // 12ab

final char value6 = 0xffff;
System.out.println(hex.toHexDigits(value6)); // ffff

String toHexDigits (int value)

Returns the eight hexadecimal characters for the int value.

final var hex = HexFormat.of();

final int value1 = 1;
System.out.println(hex.toHexDigits(value1)); // 00000001

final int value2 = 10;
System.out.println(hex.toHexDigits(value2)); // 0000000a

final int value3 = 16;
System.out.println(hex.toHexDigits(value3)); // 00000010

final int value4 = 127;
System.out.println(hex.toHexDigits(value4)); // 0000007f

final int value5 = 0x1234abcd;
System.out.println(hex.toHexDigits(value5)); // 1234abcd

final int value6 = -1;
System.out.println(hex.toHexDigits(value6)); // ffffffff

String toHexDigits (long value)

Returns the sixteen hexadecimal characters for the long value.

final var hex = HexFormat.of();

final long value1 = 1L;
System.out.println(hex.toHexDigits(value1)); // 0000000000000001

final long value2 = 10L;
System.out.println(hex.toHexDigits(value2)); // 000000000000000a

final long value3 = 16L;
System.out.println(hex.toHexDigits(value3)); // 0000000000000010

final long value4 = 127L;
System.out.println(hex.toHexDigits(value4)); // 000000000000007f

final long value5 = 0x1234567890abcdefL;
System.out.println(hex.toHexDigits(value5)); // 1234567890abcdef

final long value6 = -1L;
System.out.println(hex.toHexDigits(value6)); // ffffffffffffffff

String toHexDigits (long value, int digits)

Returns up to sixteen hexadecimal characters for the long value.

Please see also : toHexDigits(long value)

final var hex = HexFormat.of();
final long value = 0x1234567890abcdefL;

System.out.println(hex.toHexDigits(value, 0).isEmpty()); // true
System.out.println(hex.toHexDigits(value, 1)); // f
System.out.println(hex.toHexDigits(value, 2)); // ef
System.out.println(hex.toHexDigits(value, 3)); // def

System.out.println(hex.toHexDigits(value, 7)); // 0abcdef
System.out.println(hex.toHexDigits(value, 8)); // 90abcdef
System.out.println(hex.toHexDigits(value, 9)); // 890abcdef

System.out.println(hex.toHexDigits(value, 14)); // 34567890abcdef
System.out.println(hex.toHexDigits(value, 15)); // 234567890abcdef
System.out.println(hex.toHexDigits(value, 16)); // 1234567890abcdef

String toHexDigits (short value)

Returns the four hexadecimal characters for the short value.

final var hex = HexFormat.of();

final short value1 = 1;
System.out.println(hex.toHexDigits(value1)); // 0001

final short value2 = 10;
System.out.println(hex.toHexDigits(value2)); // 000a

final short value3 = 16;
System.out.println(hex.toHexDigits(value3)); // 0010

final short value4 = 127;
System.out.println(hex.toHexDigits(value4)); // 007f

final short value5 = 0x12ab;
System.out.println(hex.toHexDigits(value5)); // 12ab

final short value6 = -1;
System.out.println(hex.toHexDigits(value6)); // ffff

<A extends Appendable> A toHexDigits (A out, byte value)

Appends two hexadecimal characters for the byte value to the Appendable.

final var hex = HexFormat.of();
final var out = new StringBuilder();

final byte value1 = 1;
final var ret1 = hex.toHexDigits(out, value1);
System.out.println(ret1); // 01

final byte value2 = 2;
final var ret2 = hex.toHexDigits(out, value2);
System.out.println(ret2); // 0102

final byte value3 = 10;
final var ret3 = hex.toHexDigits(out, value3);
System.out.println(ret3); // 01020a

final byte value4 = 11;
final var ret4 = hex.toHexDigits(out, value4);
System.out.println(ret4); // 01020a0b

System.out.println(out); // 01020a0b

char toHighHexDigit (int value)

Returns the hexadecimal character for the high 4 bits of the value considering it to be a byte.

final var hex = HexFormat.of();

System.out.println(hex.toHighHexDigit(0x00)); // 0
System.out.println(hex.toHighHexDigit(0x10)); // 1
System.out.println(hex.toHighHexDigit(0x20)); // 2

System.out.println(hex.toHighHexDigit(0xa0)); // a
System.out.println(hex.toHighHexDigit(0xb0)); // b
System.out.println(hex.toHighHexDigit(0xc0)); // c

System.out.println(hex.toHighHexDigit(0x0f)); // 0
System.out.println(hex.toHighHexDigit(0x10)); // 1
System.out.println(hex.toHighHexDigit(0x11)); // 1
System.out.println(hex.toHighHexDigit(0xff)); // f

char toLowHexDigit (int value)

Returns the hexadecimal character for the low 4 bits of the value considering it to be a byte.

final var hex = HexFormat.of();

System.out.println(hex.toLowHexDigit(0x00)); // 0
System.out.println(hex.toLowHexDigit(0x01)); // 1
System.out.println(hex.toLowHexDigit(0x02)); // 2

System.out.println(hex.toLowHexDigit(0x0a)); // a
System.out.println(hex.toLowHexDigit(0x0b)); // b
System.out.println(hex.toLowHexDigit(0x0c)); // c

System.out.println(hex.toLowHexDigit(0x0f)); // f
System.out.println(hex.toLowHexDigit(0x10)); // 0
System.out.println(hex.toLowHexDigit(0x11)); // 1
System.out.println(hex.toLowHexDigit(0xff)); // f

String toString ()

Returns a description of the formatter parameters for uppercase, delimiter, prefix, and suffix.

final var ret1 = HexFormat.of().toString();
System.out.println(ret1); // uppercase: false, delimiter: "", prefix: "", suffix: ""

final var ret2 = HexFormat.ofDelimiter("-").toString();
System.out.println(ret2); // uppercase: false, delimiter: "-", prefix: "", suffix: ""

final var ret3 = HexFormat.of().withUpperCase().toString();
System.out.println(ret3); // uppercase: true, delimiter: "", prefix: "", suffix: ""

final var ret4 = HexFormat.of().withPrefix("abc").withSuffix("XYZ").toString();
System.out.println(ret4); // uppercase: false, delimiter: "", prefix: "abc", suffix: "XYZ"

HexFormat withDelimiter (String delimiter)

Returns a copy of this HexFormat with the delimiter.

final byte[] bytes = {1, 2, 3, 10, 11, 12};

final var hex1 = HexFormat.of();
final var ret1 = hex1.formatHex(bytes);
System.out.println(ret1); // 0102030a0b0c

final var hex2 = hex1.withDelimiter(", ");
final var ret2 = hex2.formatHex(bytes);
System.out.println(ret2); // 01, 02, 03, 0a, 0b, 0c

final var hex3 = hex1.withDelimiter("-");
final var ret3 = hex3.formatHex(bytes);
System.out.println(ret3); // 01-02-03-0a-0b-0c

HexFormat withLowerCase ()

Returns a copy of this HexFormat to use lowercase hexadecimal characters.

final var hex = HexFormat.of();

System.out.println(hex.isUpperCase()); // false
System.out.println(hex.toHexDigits(0x123abcd)); // 0123abcd

final var upperCaseHex = hex.withUpperCase();

System.out.println(upperCaseHex.isUpperCase()); // true
System.out.println(upperCaseHex.toHexDigits(0x123abcd)); // 0123ABCD

final var lowerCaseHex = upperCaseHex.withLowerCase();
System.out.println(lowerCaseHex.isUpperCase()); // false
System.out.println(lowerCaseHex.toHexDigits(0x123abcd)); // 0123abcd

HexFormat withPrefix (String prefix)

Returns a copy of this HexFormat with the prefix.

Please see prefix().

HexFormat withSuffix (String suffix)

Returns a copy of this HexFormat with the suffix.

Please see suffix().

HexFormat withUpperCase ()

Returns a copy of this HexFormat to use uppercase hexadecimal characters.

Please see isUpperCase().


Related posts

To top of page