Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Hexadecimal notation

One reason why humans use a system with ten digits instead of one with two digits is that it’s 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.

Counting on Our Fingers

Undoubtedly, the main reason humans use decimal notation instead of binary notation is that we have ten fingers on our hands. Indeed that’s why the word digit is used to refer to both the characters between 0 and 9 and our fingers and toes. Humans use both things to count with. Doubtless if we ever encounter a sentient alien race, their number system will be based on the number of fleshy protuberances they count with.

However, not all human societies have used decimal notation. The Mayans used a system with twenty digits, and the Babylonians had an unbelievable 60 different digits, although only two symbols were used to form each of the 60 digits in a predictable way.

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 don’t 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.

Table 2-1 Hexadecimal digit binary bit patterns

4-bit binary pattern Hexadecimal digit

0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

There’s 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 computer’s 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));


Secret:  The Byte and Short classes do not have toHexString() methods, but bytes and shorts can be converted using the Integer.toHexString() method.

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);


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.