Online Book Reader

Home Category

Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [65]

By Root 1537 0

9A48Ch = 9 x 65,536 +

A x 4096 +

4 x 256 +

8 x 16 +

C x 1

Notice that there's no ambiguity in writing the single digits of the number (9, A, 4, 8, and C) without a subscript to indicate the number base. A 9 is a 9 whether it's decimal or hexadecimal. And an A is obviously hexadecimal—equivalent to 10 in decimal.

Converting all the digits to decimal lets us actually do the calculation:

9A48Ch = 9 x 65,536 +

10 x 4096 +

4 x 256 +

8 x 16 +

12 x 1

And the answer is 631,948. This is how hexadecimal numbers are converted to decimal.

Here's a template for converting any 4-digit hexadecimal number to decimal:

For example, here's the conversion of 79ACh. Keep in mind that the hexadecimal digits A and C are decimal 10 and 12, respectively:

Converting decimal numbers to hexadecimal generally requires divisions. If the number is 255 or smaller, you know that it can be represented by 1 byte, which is two hexadecimal digits. To calculate those two digits, divide the number by 16 to get the quotient and the remainder. Let's use an earlier example—the decimal number 182. Divide 182 by 16 to get 11 (which is a B in hexadecimal) with a remainder of 6. The hexadecimal equivalent is B6h.

If the decimal number you want to convert is smaller than 65,536, the hexadecimal equivalent will have four digits or fewer. Here's a template for converting such a number to hexadecimal:

You start by putting the entire decimal number in the box in the upper left corner. That's your first dividend. Divide by 4096, the first divisor. The quotient goes in the box below the dividend, and the remainder goes in the box to the right of the dividend. That remainder is the new dividend that you divide by 256. Here's the conversion of 31,148 back to hexadecimal:

Of course, decimal numbers 10 and 12 correspond to hexadecimal A and C. The result is 79ACh.

One problem with this technique is that you probably want to use a calculator for the divisions, but calculators don't show remainders. If you divide 31,148 by 4096 on a calculator, you'll get 7.6044921875. To calculate the remainder, you need to multiply 4096 by 7 (to get 28,672) and subtract that from 31,148. Or multiply 4096 by 0.6044921875, the fractional part of the quotient. (On the other hand, some calculators can convert between decimal and hexadecimal.)

Another approach to converting decimal numbers through 65,535 to hex involves first separating the number into 2 bytes by dividing by 256. Then for each byte, divide by 16. Here's a template for doing it:

Start at the top. With each division, the quotient goes in the box to the left below the divisor, and the remainder goes in the box to the right. For example, here's the conversion of 51,966:

The hexadecimal digits are 12, 10, 15, and 14, or CAFE, which looks more like a word than a number! (And if you go there, you may want to order your coffee 56,495.)

As for every other number base, there's an addition table associated with hexadecimal:

+

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

0

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

1

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

10

2

2

3

4

5

6

7

8

9

A

B

C

D

E

F

10

11

3

3

4

5

6

7

8

9

A

B

C

D

E

F

10

11

12

4

4

5

6

7

8

9

A

B

C

D

E

F

10

11

12

13

5

5

6

7

8

9

A

B

C

D

E

F

10

11

12

13

14

6

6

7

8

9

A

B

C

D

E

F

10

11

12

13

14

15

7

7

8

9

A

B

C

D

E

F

10

11

12

13

14

15

16

8

8

9

A

B

C

D

E

F

10

11

12

13

14

15

16

17

9

9

A

B

C

D

E

F

10

11

12

13

14

15

16

17

18

A

A

B

C

D

E

F

10

11

12

13

14

15

16

17

18

19

B

B

C

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

C

C

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

D

D

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

E

E

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

F

F

10

11

12

13

14

15

16

17

18

19

1A

1B

1C

1D

1E

You can use the table and normal carry rules to add hexadecimal numbers:

You'll

Return Main Page Previous Page Next Page

®Online Book Reader