![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Hexadecimal notationOne reason why humans use a system with ten digits instead of one with two digits is that its quite hard to read numbers like 00000000100001. Converting between binary and decimal notation requires substantial arithmetic, but converting between binary and hexadecimal notation can be done with the much faster table-lookup approach.
Hexadecimal notation uses 16 digits and a place-value system based on powers of 16. As well as the customary 0 through 9, there are also A (10), B (11), C (12), D (13), E (14), and F (15). These extra digits are normally written using uppercase letters, but the lowercase a, b, c, d, e, and f are sometimes used instead. Thus in hexadecimal notation, 65 is written as 41; that is, four times 16 plus one times 1. The hexadecimal number E3 is decimal 227; 14 times 16 plus 3. There are always exactly four bits in one hexadecimal digit, so binary 0000 is always hex 0, binary 1000 is always hex 8, and binary 1111 is always hex F. This is in contrast to decimal notation, where the digits 0 through 7 can be encoded in three bits but the digits 8 and 9 require four bits. If you do use four bits for each decimal digit, you also have six 4-bit patterns that dont correspond to a digit. Table 2-1 lists the binary equivalents of the 16 hexadecimal digits. You can convert a number from binary to hexadecimal and vice versa by simple substitution of bit patterns according to this table. Conversion to decimal requires quite a bit more effort.
Theres one more advantage to a hexadecimal number system: Two hexadecimal digits equal one byte. Any single-byte value can be written as exactly two hexadecimal digits, and any pair of hexadecimal digits is exactly one byte. Therefore, hexadecimal digits are often used to represent the state of a computers memory in a compact and relatively easy-to-read fashion. Disk editors can display the contents of hard drives as sequences of hexadecimal numbers. And, as you soon learn, you can read and write Java .class byte code files by treating them as sequences of hexadecimal numbers. Hexadecimal digits are so useful in computer programming that Java even lets you write integer literals as hexadecimal digits. To use a hexadecimal literal instead of a decimal literal, prefix it with 0x or 0X. Java does not care whether you use small letters or capital letters in your hexadecimal literals. For example, the following five lines of code each say the same thing: int n = 227; int n = 0xE3; int n = 0xe3; int n = 0Xe3; int n = 0XE3; When using hexadecimal numbers, most programmers choose the form 0xE3 with a small x and capital hex digits. This is slightly easier to read and understand than the other three forms. Java has several methods to convert between decimal and hexadecimal notation. The Integer and Long classes each have a static toHexString() method that converts ints and longs respectively to hexadecimal strings. For example, to print the int value 1024 as a hexadecimal string, you could write System.out.println(Integer.toHexString(1024)); Longs are converted similarly: System.out.println(Long.toHexString(5000000000L));
You can convert a hexadecimal string to a numeric value using the parse and valueOf() methods described in the last section. Just pass 16 as the base argument instead of 2. For example: byte b = Byte.parseByte("3F", 16); short s = Short.parseShort("78A2", 16); int i = Integer.parseInt("90087FA2", 16); long l = Long.parseLong("02087FA290087FA2", 16); Byte B = Byte.valueOf("3F", 16); Short S = Short.valueOf("78A2", 16); Integer I = Integer.valueOf("90087FA2", 16); Long L = Long.valueOf("02087FA290087FA2", 16);
|
![]() |
|