HexFormat
converts between bytes and chars and hex-encoded strings which may include
additional formatting markup such as prefixes, suffixes, and delimiters.
There are two factories of HexFormat
with preset parameters of()
and
ofDelimiter(delimiter)
. For other parameter combinations
the withXXX
methods return copies of HexFormat
modified
withPrefix(String)
, withSuffix(String)
, withDelimiter(String)
or choice of withUpperCase()
or withLowerCase()
parameters.
For primitive to hexadecimal string conversions the toHexDigits
methods include toHexDigits(byte)
, toHexDigits(int)
, and
toHexDigits(long)
, etc. The default is to use lowercase characters "0-9","a-f"
.
For conversions producing uppercase hexadecimal the characters are "0-9","A-F"
.
Only the HexFormat.
parameter is
considered; the delimiter, prefix and suffix are not used.
For hexadecimal string to primitive conversions the fromHexDigits
methods include fromHexDigits(string)
,
fromHexDigitsToLong(string)
, and
fromHexDigit(int)
converts a single character or codepoint.
For conversions from hexadecimal characters the digits and uppercase and lowercase
characters in "0-9", "a-f", and "A-F"
are converted to corresponding values
0-15
. The delimiter, prefix, suffix, and uppercase parameters are not used.
For byte array to formatted hexadecimal string conversions
the formatHex
methods include formatHex(byte[])
and formatHex(Appendable, byte[])
.
The formatted output is a string or is appended to an Appendable
such as
StringBuilder
or java.
.
Each byte value is formatted as the prefix, two hexadecimal characters from the
uppercase or lowercase digits, and the suffix.
A delimiter follows each formatted value, except the last.
For conversions producing uppercase hexadecimal strings use withUpperCase()
.
For formatted hexadecimal string to byte array conversions the
parseHex
methods include parseHex(CharSequence)
and
parseHex(char[], offset, length)
.
Each byte value is parsed from the prefix, two case insensitive hexadecimal characters,
and the suffix. A delimiter follows each formatted value, except the last.
API Note
For example, an individual byte is converted to a string of hexadecimal digits using
toHexDigits(int)
and converted from a string to a
primitive value using fromHexDigits(string)
.
HexFormat hex = HexFormat.of();
byte b = 127;
String byteStr = hex.toHexDigits(b);
byte byteVal = (byte)hex.fromHexDigits(byteStr);
assert(byteStr.equals("7f"));
assert(b == byteVal);
// The hexadecimal digits are: "7f"
For a comma (", "
) separated format with a prefix ("#"
)
using lowercase hex digits the HexFormat
is:
HexFormat commaFormat = HexFormat.ofDelimiter(", ").withPrefix("#");
byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
String str = commaFormat.formatHex(bytes);
byte[] parsed = commaFormat.parseHex(str);
assert(Arrays.equals(bytes, parsed));
// The formatted string is: "#00, #01, #02, #03, #7c, #7d, #7e, #7f"
For a fingerprint of byte values that uses the delimiter colon (":"
)
and uppercase characters the HexFormat
is:
HexFormat formatFingerprint = HexFormat.ofDelimiter(":").withUpperCase();
byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
String str = formatFingerprint.formatHex(bytes);
byte[] parsed = formatFingerprint.parseHex(str);
assert(Arrays.equals(bytes, parsed));
// The formatted string is: "00:01:02:03:7C:7D:7E:7F"
This is a value-based
class; use of identity-sensitive operations (including reference equality
(==
), identity hash code, or synchronization) on instances of
HexFormat
may have unpredictable results and should be avoided.
The equals
method should be used for comparisons.
This class is immutable and thread-safe.
Unless otherwise noted, passing a null argument to any method will cause a
NullPointerException
to be thrown.
Modifier and Type | Class and Description |
---|---|
private static enum |
Modifier and Type | Field and Description |
---|---|
private final String | |
private final HexFormat. | |
private static final byte[] | |
private static final byte[] | |
private static final HexFormat | HEX_FORMAT
Format each byte of an array as a pair of hexadecimal digits. |
private static final HexFormat | |
private static final JavaLangAccess | |
private final String | |
private final String |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
private static int | Returns: the length of the rangethe initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex, int the maximum allowed limit)Check the number of requested digits against a limit. |
private static void | checkLiteral(CharSequence
a CharSequence string, int the index of the literal in the CharSequence index, String the expected literal literal)Compare the literal and throw an exception if it does not match. |
private static int | Returns: the lengththe requested size of a byte array. length)Checked that the requested size for the result string is less than or equal to the max array size. |
public String | Returns: the delimiter, non-null, may be empty"" Returns the delimiter between hexadecimal values in formatted hexadecimal strings. |
public boolean | Returns: true if the other object is a HexFormat and the parameters
uppercase, delimiter, prefix, and suffix are equal;
otherwise false an object, may be null o)Overrides java. Returns |
private static String | |
public String | Returns: a string hexadecimal formatting of the byte arraya non-null array of bytes bytes)Returns a hexadecimal string formatted from a byte array. |
public String | Returns: a string hexadecimal formatting each byte of the array rangea non-null array of bytes bytes, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive toIndex)Returns a hexadecimal string formatted from a byte array range. |
public < The type of A extends Appendable> AAppendable | Returns: theAppendable an out, byte[] Appendable , non-nulla byte array bytes)Appends formatted hexadecimal strings from a byte array to the |
public < The type of A extends Appendable> AAppendable | Returns: theAppendable an out, byte[] Appendable , non-nulla byte array, non-null bytes, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex)Appends formatted hexadecimal strings from a byte array range to the |
private String | Returns: a String formatted or null for non-single byte delimiter or non-empty prefix or suffixthe bytes, non-null bytes, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex)Returns a string formatting of the range of bytes optimized for a single allocation. |
public static int | Returns: the value0-15 a character or codepoint ch)Returns the value for the hexadecimal character or codepoint. |
private static int | Returns: the value parsed from the string rangea CharSequence containing the characters string, int the index of the first character of the range index)Returns a value parsed from two hexadecimal characters in a string. |
public static int | Returns: the value parsed from the stringa CharSequence containing up to eight hexadecimal characters string)Returns the |
public static int | Returns: the value parsed from the string rangea CharSequence containing the characters string, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex)Returns the |
public static long | Returns: the value parsed from the stringa CharSequence containing up to sixteen hexadecimal characters string)Returns the long value parsed from a string of up to sixteen hexadecimal characters. |
public static long | Returns: the value parsed from the string rangea CharSequence containing the characters string, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex)Returns the long value parsed from a string range of up to sixteen hexadecimal characters. |
public int | Returns: a hashcode for thisHexFormat Overrides java. Returns a hashcode for this |
public static boolean | Returns: true if the character is valid a hexadecimal character,
otherwise false a codepoint ch)Returns |
public boolean | Returns: true if the hexadecimal digits are uppercase,
otherwise false Returns |
public static HexFormat | Returns: a hexadecimal formatter with no delimiter and lowercase charactersReturns a hexadecimal formatter with no delimiter and lowercase characters. |
public static HexFormat | Returns: aHexFormat with the delimiter and lowercase charactersa delimiter, non-null, may be empty delimiter)Returns a hexadecimal formatter with the delimiter and lowercase characters. |
public byte[] | Returns: a byte array with the values parsed from the stringa string containing the byte values with prefix, hexadecimal digits, suffix,
and delimiters string)Returns a byte array containing hexadecimal values parsed from the string. |
public byte[] | Returns: a byte array with the values parsed from the string rangea string range containing hexadecimal digits,
delimiters, prefix, and suffix. string, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex)Returns a byte array containing hexadecimal values parsed from a range of the string. |
public byte[] | Returns: a byte array with the values parsed from the character array rangea character array range containing an even number of hexadecimal digits,
delimiters, prefix, and suffix. chars, int the initial index of the range, inclusive fromIndex, int the final index of the range, exclusive. toIndex)Returns a byte array containing hexadecimal values parsed from a range of the character array. |
private static byte[] | Returns: a byte arraya string containing an even number of only hex digits string)Returns a byte array containing the parsed hex digits. |
public String | Returns: the prefix, non-null, may be empty"" Returns the prefix used for each hexadecimal value in formatted hexadecimal strings. |
public String | Returns: the suffix, non-null, may be empty"" Returns the suffix used for each hexadecimal value in formatted hexadecimal strings. |
public < The type of A extends Appendable> AAppendable | Returns: theAppendable an out, byte Appendable , non-nulla byte value value)Appends two hexadecimal characters for the byte value to the |
public String | Returns: the two hexadecimal characters for the byte valuea byte value value)Returns the two hexadecimal characters for the |
public String | Returns: the four hexadecimal characters for thechar valuea value)char valueReturns the four hexadecimal characters for the |
public String | Returns: the four hexadecimal characters for theshort valuea value)short valueReturns the four hexadecimal characters for the |
public String | Returns: the eight hexadecimal characters for theint valuean value)int valueReturns the eight hexadecimal characters for the |
public String | Returns: the sixteen hexadecimal characters for thelong valuea value)long valueReturns the sixteen hexadecimal characters for the |
public String | Returns: the hexadecimal characters for thelong valuea value, int long valuethe number of hexadecimal digits to return, 0 to 16 digits)Returns up to sixteen hexadecimal characters for the |
public char | Returns: the hexadecimal character for the bits4-7 of the valuea value, only bits value)4-7 of the value are usedReturns the hexadecimal character for the high 4 bits of the value considering it to be a byte. |
public char | Returns: the hexadecimal character for the low 4 bits0-3 of the valuea value, only the low 4 bits value)0-3 of the value are usedReturns the hexadecimal character for the low 4 bits of the value considering it to be a byte. |
public String | Returns: a description of thisHexFormat Overrides java. Returns a description of the formatter parameters for uppercase, delimiter, prefix, and suffix. |
public HexFormat | Returns: a copy of thisHexFormat with the delimiterthe delimiter, non-null, may be empty delimiter)Returns a copy of this |
public HexFormat | Returns: a copy of thisHexFormat with lowercase hexadecimal charactersReturns a copy of this |
public HexFormat | Returns: a copy of thisHexFormat with the prefixa prefix, non-null, may be empty prefix)Returns a copy of this |
public HexFormat | Returns: a copy of thisHexFormat with the suffixa suffix, non-null, may be empty suffix)Returns a copy of this |
public HexFormat | Returns: a copy of thisHexFormat with uppercase hexadecimal charactersReturns a copy of this |
delimiter | back to summary |
---|---|
private final String delimiter |
digitCase | back to summary |
---|---|
private final HexFormat. |
DIGITS | back to summary |
---|---|
private static final byte[] DIGITS |
EMPTY_BYTES | back to summary |
---|---|
private static final byte[] EMPTY_BYTES |
HEX_FORMAT | back to summary |
---|---|
private static final HexFormat HEX_FORMAT Format each byte of an array as a pair of hexadecimal digits. The hexadecimal characters are from lowercase alpha digits. |
HEX_UPPER_FORMAT | back to summary |
---|---|
private static final HexFormat HEX_UPPER_FORMAT |
jla | back to summary |
---|---|
private static final JavaLangAccess jla |
prefix | back to summary |
---|---|
private final String prefix |
suffix | back to summary |
---|---|
private final String suffix |
HexFormat | back to summary |
---|---|
private HexFormat(String delimiter, String prefix, String suffix, HexFormat. Returns a HexFormat with a delimiter, prefix, suffix, and array of digits. |
checkDigitCount | back to summary |
---|---|
private static int checkDigitCount(int fromIndex, int toIndex, int limit) Check the number of requested digits against a limit.
|
checkLiteral | back to summary |
---|---|
private static void checkLiteral(CharSequence string, int index, String literal) Compare the literal and throw an exception if it does not match.
Pre-condition:
|
checkMaxArraySize | back to summary |
---|---|
private static int checkMaxArraySize(long length) Checked that the requested size for the result string is less than or equal to the max array size.
|
delimiter | back to summary |
---|---|
public String delimiter() Returns the delimiter between hexadecimal values in formatted hexadecimal strings.
|
equals | back to summary |
---|---|
public boolean equals(Object o) Overrides java. Returns |
escapeNL | back to summary |
---|---|
private static String escapeNL(String string) Expands new line characters to escaped newlines for display. |
formatHex | back to summary |
---|---|
public String formatHex(byte[] bytes) Returns a hexadecimal string formatted from a byte array.
Each byte value is formatted as the prefix, two hexadecimal characters
selected from uppercase or lowercase digits, and the suffix.
A delimiter follows each formatted value, except the last.
The behavior is equivalent to
|
formatHex | back to summary |
---|---|
public String formatHex(byte[] bytes, int fromIndex, int toIndex) Returns a hexadecimal string formatted from a byte array range. Each byte value is formatted as the prefix, two hexadecimal characters selected from uppercase or lowercase digits, and the suffix. A delimiter follows each formatted value, except the last.
|
formatHex | back to summary |
---|---|
public <A extends Appendable> A formatHex(A out, byte[] bytes) Appends formatted hexadecimal strings from a byte array to the
|
formatHex | back to summary |
---|---|
public <A extends Appendable> A formatHex(A out, byte[] bytes, int fromIndex, int toIndex) Appends formatted hexadecimal strings from a byte array range to the
|
formatOptDelimiter | back to summary |
---|---|
private String formatOptDelimiter(byte[] bytes, int fromIndex, int toIndex) Returns a string formatting of the range of bytes optimized for a single allocation. Prefix and suffix must be empty and the delimiter must be empty or a single byte character, otherwise null is returned.
|
fromHexDigit | back to summary |
---|---|
public static int fromHexDigit(int ch) Returns the value for the hexadecimal character or codepoint. The value is:
|
fromHexDigits | back to summary |
---|---|
private static int fromHexDigits(CharSequence string, int index) Returns a value parsed from two hexadecimal characters in a string.
The characters in the range from
|
fromHexDigits | back to summary |
---|---|
public static int fromHexDigits(CharSequence string) Returns the API Note
|
fromHexDigits | back to summary |
---|---|
public static int fromHexDigits(CharSequence string, int fromIndex, int toIndex) Returns the API Note
|
fromHexDigitsToLong | back to summary |
---|---|
public static long fromHexDigitsToLong(CharSequence string) Returns the long value parsed from a string of up to sixteen hexadecimal characters.
The hexadecimal characters are parsed from most significant to least significant
using API Note
|
fromHexDigitsToLong | back to summary |
---|---|
public static long fromHexDigitsToLong(CharSequence string, int fromIndex, int toIndex) Returns the long value parsed from a string range of up to sixteen hexadecimal
characters.
The characters in the range API Note
|
hashCode | back to summary |
---|---|
public int hashCode() Overrides java. Returns a hashcode for this
|
isHexDigit | back to summary |
---|---|
public static boolean isHexDigit(int ch) Returns
|
isUpperCase | back to summary |
---|---|
public boolean isUpperCase() Returns
|
of | back to summary |
---|---|
public static HexFormat of() Returns a hexadecimal formatter with no delimiter and lowercase characters.
The delimiter, prefix, and suffix are empty.
The methods
|
ofDelimiter | back to summary |
---|---|
public static HexFormat ofDelimiter(String delimiter) Returns a hexadecimal formatter with the delimiter and lowercase characters.
The prefix and suffix are empty.
The methods |
parseHex | back to summary |
---|---|
public byte[] parseHex(CharSequence string) Returns a byte array containing hexadecimal values parsed from the string. Each byte value is parsed from the prefix, two case insensitive hexadecimal characters, and the suffix. A delimiter follows each formatted value, except the last. The delimiters, prefixes, and suffixes strings must be present; they may be empty strings. A valid string consists only of the above format.
|
parseHex | back to summary |
---|---|
public byte[] parseHex(CharSequence string, int fromIndex, int toIndex) Returns a byte array containing hexadecimal values parsed from a range of the string. Each byte value is parsed from the prefix, two case insensitive hexadecimal characters, and the suffix. A delimiter follows each formatted value, except the last. The delimiters, prefixes, and suffixes strings must be present; they may be empty strings. A valid string consists only of the above format.
|
parseHex | back to summary |
---|---|
public byte[] parseHex(char[] chars, int fromIndex, int toIndex) Returns a byte array containing hexadecimal values parsed from a range of the character array. Each byte value is parsed from the prefix, two case insensitive hexadecimal characters, and the suffix. A delimiter follows each formatted value, except the last. The delimiters, prefixes, and suffixes strings must be present; they may be empty strings. A valid character array range consists only of the above format.
|
parseNoDelimiter | back to summary |
---|---|
private static byte[] parseNoDelimiter(CharSequence string) Returns a byte array containing the parsed hex digits. A valid string consists only of an even number of hex digits.
|
prefix | back to summary |
---|---|
public String prefix() Returns the prefix used for each hexadecimal value in formatted hexadecimal strings.
|
suffix | back to summary |
---|---|
public String suffix() Returns the suffix used for each hexadecimal value in formatted hexadecimal strings.
|
toHexDigits | back to summary |
---|---|
public <A extends Appendable> A toHexDigits(A out, byte value) Appends two hexadecimal characters for the byte value to the
|
toHexDigits | back to summary |
---|---|
public String toHexDigits(byte value) Returns the two hexadecimal characters for the
|
toHexDigits | back to summary |
---|---|
public String toHexDigits(char value) Returns the four hexadecimal characters for the
|
toHexDigits | back to summary |
---|---|
public String toHexDigits(short value) Returns the four hexadecimal characters for the
|
toHexDigits | back to summary |
---|---|
public String toHexDigits(int value) Returns the eight hexadecimal characters for the
|
toHexDigits | back to summary |
---|---|
public String toHexDigits(long value) Returns the sixteen hexadecimal characters for the
|
toHexDigits | back to summary |
---|---|
public String toHexDigits(long value, int digits) Returns up to sixteen hexadecimal characters for the
|
toHighHexDigit | back to summary |
---|---|
public char toHighHexDigit(int value) Returns the hexadecimal character for the high 4 bits of the value considering it to be a byte.
If the parameter
|
toLowHexDigit | back to summary |
---|---|
public char toLowHexDigit(int value) Returns the hexadecimal character for the low 4 bits of the value considering it to be a byte.
If the parameter
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a description of the formatter parameters for uppercase, delimiter, prefix, and suffix. |
withDelimiter | back to summary |
---|---|
public HexFormat withDelimiter(String delimiter) Returns a copy of this |
withLowerCase | back to summary |
---|---|
public HexFormat withLowerCase() Returns a copy of this
|
withPrefix | back to summary |
---|---|
public HexFormat withPrefix(String prefix) Returns a copy of this |
withSuffix | back to summary |
---|---|
public HexFormat withSuffix(String suffix) Returns a copy of this |
withUpperCase | back to summary |
---|---|
public HexFormat withUpperCase() Returns a copy of this
|
Modifier and Type | Field and Description |
---|---|
public static final HexFormat. | |
public static final HexFormat. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static HexFormat. | |
public static HexFormat. |
LOWERCASE | back to summary |
---|---|
public static final HexFormat. |
UPPERCASE | back to summary |
---|---|
public static final HexFormat. |
Case | back to summary |
---|---|
private Case() |
valueOf | back to summary |
---|---|
public static HexFormat. |
values | back to summary |
---|---|
public static HexFormat. |