Java Reference
In-Depth Information
Note
The sample code prints to the console. Your console may not print all the char-
acter glyphs shown in this example because of font or platform differences. However,
the characters will be correctly converted to integers.
How It Works
The Unicode character set is large, containing more than a million unique code points
with integer values ranging from
0x0000
through
0x10FFFF
. Each character value
has a set of properties. One of the properties is
isDigit
. If this property is
true
, the
character represents a numeric digit from
0
through
9
. For example, the characters
with code point values
0x30
through
0x39
have the character glyphs
0
,
1
,
2
,
3
,
4,
5
,
6
,
7
,
8
, and
9
. If you simply convert these code values to their corresponding in-
teger values, you would get the hexadecimal values
0x30
through
0x39
. The corres-
ponding decimal values are
48
through
57
. However, these characters also represent
numeric digits. When using them in calculations, these characters represent the values
0
through
9
.
When a character has the
digit
property, use the
Character.digit()
static
method to convert it to its corresponding integer digit value. Note that the
digit()
method is overloaded to accept either
char
or
int
arguments. Additionally, the meth-
od requires a radix. Common values for the radix are
2
,
10
, and
16
. Interestingly, al-
though the characters
a-f
and
A-F
do not have the digit property, they can be used as
digits using radix 16. For these characters, the
digit()
method returns the expected
integer values
10
through
15
.
A complete understanding of the Unicode character set and Java's implementation
requires familiarity with several new terms: character, code point, char, encoding, seri-
alization encoding, UTF-8, and UTF-16. These terms are beyond the scope of this re-
cipe, but you can learn more about these and other Unicode concepts from the Unicode
mentation.
12-2. Creating and Working with Locales
Problem