Page 494 - Introduction to Programming with Java: A Problem Solving Approach
P. 494

                460 Chapter 11 Type Details and Alternate Coding Mechanisms
decided that 7 bits would be sufficient to represent all the characters needed in a computer program. 27 􏰂 128, so 7 bits can represent 128 unique values.
Since the ASCII table was and is such a popular standard with many programming languages, the Unicode designers decided to use the ASCII character set as a subset of the Unicode character set. They inserted the ASCII character set’s characters in the first 128 slots of the Unicode character set. That means programmers can find those characters’ numeric values by referring to a simple ASCII table; they don’t have to wade through the enormous Unicode character set.
Hexadecimal Numbers
Normal numbers are expressed as powers of 10, but since computers are binary and 16 is a simple power of two (16 􏰂 24), it’s common practice to express computer quantities in base 16 (using powers of 16), rather than base 10 (using powers of 10). Base 10 numbers are called decimal numbers. Base 16 numbers are called hexadecimal numbers. The places in decimal numbers are called digits. The places in hexadecimal numbers are sometimes called hexits, but more often, they’re simply called hexadecimal digits. Base 10 numbers use 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Base 16 numbers use the 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f (uppercase letters A through F are considered to be equivalent to a through f). Thus, hexadeci- mal numbers frequently include one or more of the first six alphabetic characters as well as one or more of the normal numerical characters.
In Java, any integer can be written in either decimal or hexadecimal. If you want a number to be in-
terpreted as hexadecimal, you must prefix it with the character pair, 0x. So, if you see something like
0x263A, for example, you can recognize it as a hexadecimal number. For most of us, hexadecimal numbers
are not very intuitive. It’s pretty easy to make conversions, however. Just use Integer’s two-parameter
Apago PDF Enhancer
toString method:
Integer.toString(<starting-number>, <desired-base>)
For example, if you want to see the decimal equivalent of 0x263A, write this: System.out.println(Integer.toString(0x263A, 10));
This generates an output of 9786. Conversely, if you want to see the hexadecimal equivalent of 9786, write this:
System.out.println(Integer.toString(9786, 16));
This generates an output of 263a. Notice that this method’s output does not include the 0x prefix, and it uses lowercase letters for the alphabetic hexadecimal digits.
Unicode Escape Sequence
Whenever you write an integer, you can write it in either decimal format or hexadecimal format. Likewise, you can specify a character by writing its numeric value in either decimal format or hexadecimal format and then casting it with the (char) cast operator. Java also provides another way to specify a character. You can use the Unicode escape sequence. The Unicode escape sequence is \u followed immediately by the hexadecimal digits of a hexadecimal number. Here’s what we’re talking about:
   '\u####'
This is a single character.
Each # stands for one hexadecimal digit. We elected to show this in single quotes, not double quotes, to emphasize that the 6-element escape sequence is just a single character, not a string. It’s just like any other
 













































































   492   493   494   495   496