Вы находитесь на странице: 1из 4

Converting Between Integer Representations -- Technical Notes http://academic.evergreen.edu/projects/biophysics/technotes/program/in...

Programming

Converting Between Integer Representations


Comparison of Representations
Either Hexadecimal, Binary or Octal to Decimal
Decimal to Binary
Decimal to Hexadecimal
Decimal to Octal
Binary and Hexadecimal
Binary and Octal

Comparison of Representations

System Digits
Binary (Base 2): 0, 1
Octal (Base 8): 0, 1, 2, 3, 4, 5, 6, 7
Decimal (Base 10): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Hexadecimal (Base 16): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Decimal Hexadecimal Binary Octal

0 00 0000 0000 0

1 01 0000 0001 1

2 02 0000 0010 2

3 03 0000 0011 3

4 04 0000 0100 4

5 05 0000 0101 5

6 06 0000 0110 6

7 07 0000 0111 7

8 08 0000 1000 10

9 09 0000 1001 11

10 0A 0000 1010 12

11 0B 0000 1011 13

12 0C 0000 1100 14

13 0D 0000 1101 15

14 0E 0000 1110 16

15 0F 0000 1111 17

1 of 4 31/10/2009 10:46 PM
Converting Between Integer Representations -- Technical Notes http://academic.evergreen.edu/projects/biophysics/technotes/program/in...

16 10 0001 0000 20

17 11 0001 0001 21

18 12 0001 0010 22

... ... ... ...

255 FF 1111 1111 377

Converting from either Hexadecimal, Binary or Octal to Decimal

A binary, a hexadecimal or an octal number can be expressed as the sum of the successive powers of the bas
2, 16, or 8, respectively), with the coefficients being the digits.

For example,

Decimal 1111(base 10) = 1 1 1 1 = (1*1000) + (1*100) + (1*10) +


(1*1) = 1111(base 10)
1000 100 10 1
(103) (102) (101) (100)

Hexadecimal 1111(base 16) = 1 1 1 1 = (1*4096) + (1*256) + (1*16) +


(1*1) = 4369(base 10)
4096 256 16 1
(163) (162) (161) (160)

Binary 1111(base 2) = 1 1 1 1 = (1*8) + (1*4) + (1*2) +


(1*1) = 15(base 10)
8 4 2 1
(23) (22) (21) (20)

Octal 1111(base 8) = 1 1 1 1 = (1*512) + (1*64) + (1*8) +


(1*1) = 585(base 10)
512 64 8 1
(83) (82) (81) (80)

Converting from Decimal to Binary

To convert a decimal number into a binary number, divide it by 2 repeatedly and note the remainders. The
remainders are the bits of the binary number. The last remainder is the most significant bit, and the first rem
the least significant bit.

For example,
13(base 10) = 1101(base 2) 13 ÷ 2 = 6 remainder 1 LSB
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1 MSB

2 of 4 31/10/2009 10:46 PM
Converting Between Integer Representations -- Technical Notes http://academic.evergreen.edu/projects/biophysics/technotes/program/in...

Converting from Decimal to Hexadecimal

There are two different ways to convert a decimal number into a hexadecimal number.

1. The first method is similar to converting a decimal to a binary and involves dividing the number by decim
and noting the remainders. The first remainder is the least significant digit and the last remainder is the most
significant digit.

For example,
4620(base 10) = 120C(base 16) 4620 ÷ 16 = 288 remainder 12 = C LSB
288 ÷ 16 = 18 remainder 0
18 ÷ 16 = 1 remainder 2
1 ÷ 16 = 0 remainder 1 MSB

2. The second method involves converting the decimal number into a binary, then convert the binary in
hexadecimal number. To change the representation of the binary number to hexadecimal, separate the digits
4-bit groups beginning with the least significant bit. Then write the hexadecimal equivalent of each group.

For example,
23(base 10) = 17(base 16)

23(base 10) = 0001 0111(base 2) 23 ÷ 2 = 11 remainder 1 LSB


11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1 MSB

0001 0111(base 2) = 17(base 16) 0001 0111 (base 2)

1 7 (base 16)

Converting from Decimal to Octal

To convert a decimal number into an octal number, divide it by 8 repeatedly and note the remainders. The
remainders are the digits of the octal number. The last remainder is the most significant digit, and the first re
is the least significant digit.

For example,
1701(base 10) = 3245(base 8) 1701 ÷ 8 = 212 remainder 5 LSB
212 ÷ 8 = 26 remainder 4
26 ÷ 8 = 3 remainder 2
3 ÷ 8 = 0 remainder 3 MSB

Converting between Binary and Hexadecimal

3 of 4 31/10/2009 10:46 PM
Converting Between Integer Representations -- Technical Notes http://academic.evergreen.edu/projects/biophysics/technotes/program/in...

From Binary to Hexadecimal: To convert a binary number into its hexadecimal form, start by grouping th
into 4-bit groups. Beginning with the least significant bit (all the way to the right of the number), write the
hexadecimal equivalent of each group.

For example,

1001111101100101(base 2) = 9F65(base 16) 1001 1111 0110 0101 (base 2)

9 F 6 5 (base 16)
MSB LSB

From Hexadecimal to Binary: To convert a hexadecimal number into a binary, just reverse the above proc
starting all the way to the right, convert each digit into a 4-bit binary number.

Converting between Binary and Octal

From Binary to Octal: To convert a binary number into its octal form, start by grouping the digits into 3-bi
Beginning with the least significant bit (all the way to the right of the number), write the octal equivalent of
group.

For example,

10110111011(base 2) = 2673(base 8) 010 110 111 011 (base 2)

2 6 7 3 (base 8)
MSB LSB

From Octal to Binary: To convert an octal number into a binary, just reverse the above process; starting a
way to the right, convert each digit into a 3-bit binary number.

[ Index | Technical Notes ]

DISCLAIMER

Page author: Dawn Rorvik (rorvikd@evergreen.edu)


Last modified: 05/20/2003

4 of 4 31/10/2009 10:46 PM

Вам также может понравиться