HexFormat
public
final
class
HexFormat
extends Object
java.lang.Object | |
↳ | java.util.HexFormat |
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(java.lang.String)
, withSuffix(java.lang.String)
, withDelimiter(java.lang.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.isUpperCase()
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 PrintStream
.
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.
Summary
Public methods | |
---|---|
String
|
delimiter()
Returns the delimiter between hexadecimal values in formatted hexadecimal strings. |
boolean
|
equals(Object o)
Returns |
<A extends Appendable>
A
|
formatHex(A out, byte[] bytes)
Appends formatted hexadecimal strings from a byte array to the |
String
|
formatHex(byte[] bytes)
Returns a hexadecimal string formatted from a byte array. |
String
|
formatHex(byte[] bytes, int fromIndex, int toIndex)
Returns a hexadecimal string formatted from a byte array range. |
<A extends Appendable>
A
|
formatHex(A out, byte[] bytes, int fromIndex, int toIndex)
Appends formatted hexadecimal strings from a byte array range to the |
static
int
|
fromHexDigit(int ch)
Returns the value for the hexadecimal character or codepoint. |
static
int
|
fromHexDigits(CharSequence string, int fromIndex, int toIndex)
Returns the |
static
int
|
fromHexDigits(CharSequence string)
Returns the |
static
long
|
fromHexDigitsToLong(CharSequence string, int fromIndex, int toIndex)
Returns the long value parsed from a string range of up to sixteen hexadecimal characters. |
static
long
|
fromHexDigitsToLong(CharSequence string)
Returns the long value parsed from a string of up to sixteen hexadecimal characters. |
int
|
hashCode()
Returns a hashcode for this |
static
boolean
|
isHexDigit(int ch)
Returns |
boolean
|
isUpperCase()
Returns |
static
HexFormat
|
of()
Returns a hexadecimal formatter with no delimiter and lowercase characters. |
static
HexFormat
|
ofDelimiter(String delimiter)
Returns a hexadecimal formatter with the delimiter and lowercase characters. |
byte[]
|
parseHex(CharSequence string)
Returns a byte array containing hexadecimal values parsed from the string. |
byte[]
|
parseHex(CharSequence string, int fromIndex, int toIndex)
Returns a byte array containing hexadecimal values parsed from a range of the string. |
byte[]
|
parseHex(char[] chars, int fromIndex, int toIndex)
Returns a byte array containing hexadecimal values parsed from a range of the character array. |
String
|
prefix()
Returns the prefix used for each hexadecimal value in formatted hexadecimal strings. |
String
|
suffix()
Returns the suffix used for each hexadecimal value in formatted hexadecimal strings. |
String
|
toHexDigits(short value)
Returns the four hexadecimal characters for the |
String
|
toHexDigits(byte value)
Returns the two hexadecimal characters for the |
String
|
toHexDigits(int value)
Returns the eight hexadecimal characters for the |
String
|
toHexDigits(char value)
Returns the four hexadecimal characters for the |
<A extends Appendable>
A
|
toHexDigits(A out, byte value)
Appends two hexadecimal characters for the byte value to the |
String
|
toHexDigits(long value, int digits)
Returns up to sixteen hexadecimal characters for the |
String
|
toHexDigits(long value)
Returns the sixteen hexadecimal characters for the |
char
|
toHighHexDigit(int value)
Returns the hexadecimal character for the high 4 bits of the value considering it to be a byte. |
char
|
toLowHexDigit(int value)
Returns the hexadecimal character for the low 4 bits of the value considering it to be a byte. |
String
|
toString()
Returns a description of the formatter parameters for uppercase, delimiter, prefix, and suffix. |
HexFormat
|
withDelimiter(String delimiter)
Returns a copy of this |
HexFormat
|
withLowerCase()
Returns a copy of this |
HexFormat
|
withPrefix(String prefix)
Returns a copy of this |
HexFormat
|
withSuffix(String suffix)
Returns a copy of this |
HexFormat
|
withUpperCase()
Returns a copy of this |
Inherited methods | |
---|---|
Public methods
delimiter
public String delimiter ()
Returns the delimiter between hexadecimal values in formatted hexadecimal strings.
Returns | |
---|---|
String |
the delimiter, non-null, may be empty "" |
equals
public boolean equals (Object o)
Returns true
if the other object is a HexFormat
with the same parameters.
Parameters | |
---|---|
o |
Object : an object, may be null |
Returns | |
---|---|
boolean |
true if the other object is a HexFormat and the parameters
uppercase, delimiter, prefix, and suffix are equal;
otherwise false |
formatHex
public A formatHex (A out, byte[] bytes)
Appends formatted hexadecimal strings from a byte array to the Appendable
.
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 formatted hexadecimal strings are appended in zero or more calls to the Appendable
methods.
Parameters | |
---|---|
out |
A : an Appendable , non-null |
bytes |
byte : a byte array |
Returns | |
---|---|
A |
the Appendable |
Throws | |
---|---|
UncheckedIOException |
if an I/O exception occurs appending to the output |
formatHex
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(bytes, 0, bytes.length))
.
Parameters | |
---|---|
bytes |
byte : a non-null array of bytes |
Returns | |
---|---|
String |
a string hexadecimal formatting of the byte array |
formatHex
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.
Parameters | |
---|---|
bytes |
byte : a non-null array of bytes |
fromIndex |
int : the initial index of the range, inclusive |
toIndex |
int : the final index of the range, exclusive |
Returns | |
---|---|
String |
a string hexadecimal formatting each byte of the array range |
Throws | |
---|---|
IndexOutOfBoundsException |
if the array range is out of bounds |
formatHex
public A formatHex (A out, byte[] bytes, int fromIndex, int toIndex)
Appends formatted hexadecimal strings from a byte array range to the Appendable
.
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 formatted hexadecimal strings are appended in zero or more calls to the Appendable
methods.
Parameters | |
---|---|
out |
A : an Appendable , non-null |
bytes |
byte : a byte array, non-null |
fromIndex |
int : the initial index of the range, inclusive |
toIndex |
int : the final index of the range, exclusive. |
Returns | |
---|---|
A |
the Appendable |
Throws | |
---|---|
IndexOutOfBoundsException |
if the array range is out of bounds |
UncheckedIOException |
if an I/O exception occurs appending to the output |
fromHexDigit
public static int fromHexDigit (int ch)
Returns the value for the hexadecimal character or codepoint. The value is:
(ch - '0')
for'0'
through'9'
inclusive,(ch - 'A' + 10)
for'A'
through'F'
inclusive, and(ch - 'a' + 10)
for'a'
through'f'
inclusive.
Parameters | |
---|---|
ch |
int : a character or codepoint |
Returns | |
---|---|
int |
the value 0-15 |
Throws | |
---|---|
NumberFormatException |
if the codepoint is not a hexadecimal character |
fromHexDigits
public static int fromHexDigits (CharSequence string, int fromIndex, int toIndex)
Returns the int
value parsed from a string range of up to eight hexadecimal
characters.
The characters in the range fromIndex
to toIndex
, exclusive,
are parsed from most significant to least significant
using fromHexDigit(int)
to form an unsigned value.
The value is zero extended to 32 bits and is returned as an int
.
API Note:
Integer.parseInt(s, 16)
andInteger.parseUnsignedInt(s, 16)
are similar but allow all Unicode hexadecimal digits defined byCharacter.digit(ch, 16)
.HexFormat
uses only hexadecimal characters"0-9"
,"A-F"
and"a-f"
. Signed hexadecimal strings can be parsed withInteger#parseInt(String, int)
.
Parameters | |
---|---|
string |
CharSequence : a CharSequence containing the characters |
fromIndex |
int : the initial index of the range, inclusive |
toIndex |
int : the final index of the range, exclusive. |
Returns | |
---|---|
int |
the value parsed from the string range |
Throws | |
---|---|
IndexOutOfBoundsException |
if the range is out of bounds
for the CharSequence |
IllegalArgumentException |
if length of the range is greater than eight (8) or if any of the characters is not a hexadecimal character |
fromHexDigits
public static int fromHexDigits (CharSequence string)
Returns the int
value parsed from a string of up to eight hexadecimal characters.
The hexadecimal characters are parsed from most significant to least significant
using fromHexDigit(int)
to form an unsigned value.
The value is zero extended to 32 bits and is returned as an int
.
API Note:
Integer.parseInt(s, 16)
andInteger.parseUnsignedInt(s, 16)
are similar but allow all Unicode hexadecimal digits defined byCharacter.digit(ch, 16)
.HexFormat
uses only hexadecimal characters"0-9"
,"A-F"
and"a-f"
. Signed hexadecimal strings can be parsed withInteger#parseInt(String, int)
.
Parameters | |
---|---|
string |
CharSequence : a CharSequence containing up to eight hexadecimal characters |
Returns | |
---|---|
int |
the value parsed from the string |
Throws | |
---|---|
IllegalArgumentException |
if the string length is greater than eight (8) or if any of the characters is not a hexadecimal character |
fromHexDigitsToLong
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 fromIndex
to toIndex
, exclusive,
are parsed from most significant to least significant
using fromHexDigit(int)
to form an unsigned value.
The value is zero extended to 64 bits and is returned as a long
.
API Note:
Long.parseLong(s, 16)
andLong.parseUnsignedLong(s, 16)
are similar but allow all Unicode hexadecimal digits defined byCharacter.digit(ch, 16)
.HexFormat
uses only hexadecimal characters"0-9"
,"A-F"
and"a-f"
. Signed hexadecimal strings can be parsed withLong#parseLong(String, int)
.
Parameters | |
---|---|
string |
CharSequence : a CharSequence containing the characters |
fromIndex |
int : the initial index of the range, inclusive |
toIndex |
int : the final index of the range, exclusive. |
Returns | |
---|---|
long |
the value parsed from the string range |
Throws | |
---|---|
IndexOutOfBoundsException |
if the range is out of bounds
for the CharSequence |
IllegalArgumentException |
if the length of the range is greater than sixteen (16) or if any of the characters is not a hexadecimal character |
fromHexDigitsToLong
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 fromHexDigit(int)
to form an unsigned value.
The value is zero extended to 64 bits and is returned as a long
.
API Note:
Long.parseLong(s, 16)
andLong.parseUnsignedLong(s, 16)
are similar but allow all Unicode hexadecimal digits defined byCharacter.digit(ch, 16)
.HexFormat
uses only hexadecimal characters"0-9"
,"A-F"
and"a-f"
. Signed hexadecimal strings can be parsed withLong#parseLong(String, int)
.
Parameters | |
---|---|
string |
CharSequence : a CharSequence containing up to sixteen hexadecimal characters |
Returns | |
---|---|
long |
the value parsed from the string |
Throws | |
---|---|
IllegalArgumentException |
if the string length is greater than sixteen (16) or if any of the characters is not a hexadecimal character |
hashCode
public int hashCode ()
Returns a hashcode for this HexFormat
.
Returns | |
---|---|
int |
a hashcode for this HexFormat |
isHexDigit
public static boolean isHexDigit (int ch)
Returns true
if the character is a valid hexadecimal character or codepoint.
The valid hexadecimal characters are:
'0' ('\u0030')
through'9' ('\u0039')
inclusive,'A' ('\u0041')
through'F' ('\u0046')
inclusive, and'a' ('\u0061')
through'f' ('\u0066')
inclusive.
Parameters | |
---|---|
ch |
int : a codepoint |
Returns | |
---|---|
boolean |
true if the character is valid a hexadecimal character,
otherwise false |
isUpperCase
public boolean isUpperCase ()
Returns true
if the hexadecimal digits are uppercase,
otherwise false
.
Returns | |
---|---|
boolean |
true if the hexadecimal digits are uppercase,
otherwise false |
of
public static HexFormat of ()
Returns a hexadecimal formatter with no delimiter and lowercase characters.
The delimiter, prefix, and suffix are empty.
The methods withDelimiter
,
withUpperCase
, withLowerCase
,
withPrefix
, and withSuffix
return copies of formatters with new parameters.
Returns | |
---|---|
HexFormat |
a hexadecimal formatter with no delimiter and lowercase characters |
ofDelimiter
public static HexFormat ofDelimiter (String delimiter)
Returns a hexadecimal formatter with the delimiter and lowercase characters.
The prefix and suffix are empty.
The methods withDelimiter
,
withUpperCase
, withLowerCase
,
withPrefix
, and withSuffix
return copies of formatters with new parameters.
Parameters | |
---|---|
delimiter |
String : a delimiter, non-null, may be empty |
Returns | |
---|---|
HexFormat |
a HexFormat with the delimiter and lowercase characters |
parseHex
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.
Parameters | |
---|---|
string |
CharSequence : a string containing the byte values with prefix, hexadecimal digits, suffix,
and delimiters |
Returns | |
---|---|
byte[] |
a byte array with the values parsed from the string |
Throws | |
---|---|
IllegalArgumentException |
if the prefix or suffix is not present for each byte value, the byte values are not hexadecimal characters, or if the delimiter is not present after all but the last byte value |
parseHex
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.
Parameters | |
---|---|
string |
CharSequence : a string range containing hexadecimal digits,
delimiters, prefix, and suffix. |
fromIndex |
int : the initial index of the range, inclusive |
toIndex |
int : the final index of the range, exclusive. |
Returns | |
---|---|
byte[] |
a byte array with the values parsed from the string range |
Throws | |
---|---|
IllegalArgumentException |
if the prefix or suffix is not present for each byte value, the byte values are not hexadecimal characters, or if the delimiter is not present after all but the last byte value |
IndexOutOfBoundsException |
if the string range is out of bounds |
parseHex
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.
Parameters | |
---|---|
chars |
char : a character array range containing an even number of hexadecimal digits,
delimiters, prefix, and suffix. |
fromIndex |
int : the initial index of the range, inclusive |
toIndex |
int : the final index of the range, exclusive. |
Returns | |
---|---|
byte[] |
a byte array with the values parsed from the character array range |
Throws | |
---|---|
IllegalArgumentException |
if the prefix or suffix is not present for each byte value, the byte values are not hexadecimal characters, or if the delimiter is not present after all but the last byte value |
IndexOutOfBoundsException |
if the character array range is out of bounds |
prefix
public String prefix ()
Returns the prefix used for each hexadecimal value in formatted hexadecimal strings.
Returns | |
---|---|
String |
the prefix, non-null, may be empty "" |
suffix
public String suffix ()
Returns the suffix used for each hexadecimal value in formatted hexadecimal strings.
Returns | |
---|---|
String |
the suffix, non-null, may be empty "" |
toHexDigits
public String toHexDigits (short value)
Returns the four hexadecimal characters for the short
value.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
value |
short : a short value |
Returns | |
---|---|
String |
the four hexadecimal characters for the short value |
toHexDigits
public String toHexDigits (byte value)
Returns the two hexadecimal characters for the byte
value.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
value |
byte : a byte value |
Returns | |
---|---|
String |
the two hexadecimal characters for the byte value |
toHexDigits
public String toHexDigits (int value)
Returns the eight hexadecimal characters for the int
value.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
value |
int : an int value |
Returns | |
---|---|
String |
the eight hexadecimal characters for the int value |
See also:
toHexDigits
public String toHexDigits (char value)
Returns the four hexadecimal characters for the char
value.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
value |
char : a char value |
Returns | |
---|---|
String |
the four hexadecimal characters for the char value |
toHexDigits
public A toHexDigits (A out, byte value)
Appends two hexadecimal characters for the byte value to the Appendable
.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The hexadecimal characters are appended in one or more calls to the
Appendable
methods. The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
out |
A : an Appendable , non-null |
value |
byte : a byte value |
Returns | |
---|---|
A |
the Appendable |
Throws | |
---|---|
UncheckedIOException |
if an I/O exception occurs appending to the output |
toHexDigits
public String toHexDigits (long value, int digits)
Returns up to sixteen hexadecimal characters for the long
value.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
value |
long : a long value |
digits |
int : the number of hexadecimal digits to return, 0 to 16 |
Returns | |
---|---|
String |
the hexadecimal characters for the long value |
Throws | |
---|---|
IllegalArgumentException |
if digits is negative or greater than 16 |
toHexDigits
public String toHexDigits (long value)
Returns the sixteen hexadecimal characters for the long
value.
Each nibble (4 bits) from most significant to least significant of the value
is formatted as if by toLowHexDigit(nibble)
.
The delimiter, prefix and suffix are not used.
Parameters | |
---|---|
value |
long : a long value |
Returns | |
---|---|
String |
the sixteen hexadecimal characters for the long value |
See also:
toHighHexDigit
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 isUpperCase()
is true
the
character returned for values 10-15
is uppercase "A-F"
,
otherwise the character returned is lowercase "a-f"
.
The values in the range 0-9
are returned as "0-9"
.
Parameters | |
---|---|
value |
int : a value, only bits 4-7 of the value are used |
Returns | |
---|---|
char |
the hexadecimal character for the bits 4-7 of the value |
toLowHexDigit
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 isUpperCase()
is true
the
character returned for values 10-15
is uppercase "A-F"
,
otherwise the character returned is lowercase "a-f"
.
The values in the range 0-9
are returned as "0-9"
.
Parameters | |
---|---|
value |
int : a value, only the low 4 bits 0-3 of the value are used |
Returns | |
---|---|
char |
the hexadecimal character for the low 4 bits 0-3 of the value |
toString
public String toString ()
Returns a description of the formatter parameters for uppercase, delimiter, prefix, and suffix.
Returns | |
---|---|
String |
a description of this HexFormat |
withDelimiter
public HexFormat withDelimiter (String delimiter)
Returns a copy of this HexFormat
with the delimiter.
Parameters | |
---|---|
delimiter |
String : the delimiter, non-null, may be empty |
Returns | |
---|---|
HexFormat |
a copy of this HexFormat with the delimiter |
withLowerCase
public HexFormat withLowerCase ()
Returns a copy of this HexFormat
to use lowercase hexadecimal characters.
The lowercase hexadecimal characters are "0-9", "a-f"
.
Returns | |
---|---|
HexFormat |
a copy of this HexFormat with lowercase hexadecimal characters |
withPrefix
public HexFormat withPrefix (String prefix)
Returns a copy of this HexFormat
with the prefix.
Parameters | |
---|---|
prefix |
String : a prefix, non-null, may be empty |
Returns | |
---|---|
HexFormat |
a copy of this HexFormat with the prefix |
withSuffix
public HexFormat withSuffix (String suffix)
Returns a copy of this HexFormat
with the suffix.
Parameters | |
---|---|
suffix |
String : a suffix, non-null, may be empty |
Returns | |
---|---|
HexFormat |
a copy of this HexFormat with the suffix |
withUpperCase
public HexFormat withUpperCase ()
Returns a copy of this HexFormat
to use uppercase hexadecimal characters.
The uppercase hexadecimal characters are "0-9", "A-F"
.
Returns | |
---|---|
HexFormat |
a copy of this HexFormat with uppercase hexadecimal characters |