NumberHexFormat
Represents hexadecimal format options for formatting and parsing a numeric value.
The formatting result consist of
prefix
string, hexadecimal representation of the value being formatted, and
suffix
string.
Hexadecimal representation of a value is calculated by mapping each four-bit chunk
of its binary representation to the corresponding hexadecimal digit, starting with the most significant bits.
upperCase
determines whether upper case
0-9
,
A-F
or lower case
0-9
,
a-f
hexadecimal digits are used.
If
removeLeadingZeros
it
true
, leading zeros in the hexadecimal representation are removed.
For example, the binary representation of the
Byte
value
58
is the 8-bit long
00111010
,
which converts to a hexadecimal representation of
3a
or
3A
depending on
upperCase
.
Whereas, the binary representation of the
Int
value
58
is the 32-bit long
00000000000000000000000000111010
,
which converts to a hexadecimal representation of
0000003a
or
0000003A
depending on
upperCase
.
If
removeLeadingZeros
it
true
, leading zeros in
0000003a
are removed, resulting
3a
.
To convert a value to hexadecimal string of a particular length,
first convert the value to a type with the corresponding bit size.
For example, to convert an
Int
value to 4-digit hexadecimal string,
convert the value
toShort()
before hexadecimal formatting.
To convert it to hexadecimal string of at most 4 digits
without leading zeros, set
removeLeadingZeros
to
true
in addition.
Parsing requires prefix and suffix to be present in the input string, and the amount of hexadecimal digits to be at least one and at most the value bit size divided by four. Parsing is performed in case-insensitive manner, and removeLeadingZeros is ignored as well.
See NumberHexFormat.Builder to find out how the options are configured, and what is the default value of each option.
Types
Builder
A context for building a NumberHexFormat . Provides API for configuring format options.
class
Builder
Properties
prefix
The string that immediately precedes hexadecimal representation of a numeric value.
val
prefix
:
String
removeLeadingZeros
Specifies whether to remove leading zeros in the hexadecimal representation of a numeric value.
val
removeLeadingZeros
:
Boolean
suffix
The string that immediately succeeds hexadecimal representation of a numeric value.
val
suffix
:
String
Functions
toString
Returns a string representation of the object.
fun
toString
(
)
:
String