Java Reference
In-Depth Information
Place
value
Number
Digit
256
1967
7
1967 10 = 7AF 16
16
175
A
1
15
F
FIGURE B.6 Converting a decimal value into hexadecimal
converting the new binary number back to decimal to make sure we get the origi-
nal value.
This process works to convert any decimal value to any target base. For each
target base, the place values and possible digits change. If you start with the cor-
rect place value, each division operation will yield a valid digit in the new base.
In the example in Figure B.5, the only digits that could have resulted from each
division operation would have been 1 or 0, since we were converting to binary.
However, when we are converting to other bases, any valid digit in the new base
could result. For example, Figure B.6 shows the process of converting the decimal
value 1967 into hexadecimal.
The place value of 256, which is 16 2 , is the highest place value less than or
equal to the original number, since the next highest place value is 16 3 or 4096.
Dividing 1967 by 256 yields 7 with 175 remaining. Dividing 175 by 16 yields
10 with 15 remaining. Remember that 10 in decimal can be represented as the
single digit A in hexadecimal. The 15 remaining can be represented as the digit F.
Therefore 1967 10 is equivalent to 7AF in hexadecimal.
Shortcut Conversions
We have established techniques for converting any value in any base to
its equivalent representation in base 10, and from base 10 to any other base.
Therefore. you can now convert a number in any base to any other base by going
through base 10. However, an interesting relationship exists between the bases
that are powers of 2, such as binary, octal, and hexadecimal, which allows very
quick conversions between them.
To convert from binary to hexadecimal, for instance, you can simply group the
bits of the original value into groups of four, starting from the right, then convert
each group of four into a single hexadecimal digit. The example in Figure B.7
demonstrates this process.
Search WWH ::




Custom Search