I know everyone hates ads. But please understand that I am providing premium content for free that takes hundreds of hours of time to research and write. I don't want to go to a pay-only model like some sites, but when more and more people block ads, I end up working for free. And I have a family to support, just like you. :)

If you like The TCP/IP Guide, please consider the download version. It's priced very economically and you can read all of it in a convenient format without ads.

If you want to use this site for free, I'd be grateful if you could add the site to the whitelist for Adblock. To do so, just open the Adblock menu and select "Disable on tcpipguide.com". Or go to the Tools menu and select "Adblock Plus Preferences...". Then click "Add Filter..." at the bottom, and add this string: "@@||tcpipguide.com^\$document". Then just click OK.

Sincerely, Charles Kozierok
Author and Publisher, The TCP/IP Guide @import url(//www.google.com/cse/api/branding.css); Custom Search

Decimal, Binary, Octal and Hexadecimal Number Conversion
(Page 4 of 4)

Conversions From Decimal to Binary, Octal and Hexadecimal

Now let’s consider conversions from decimal. These require that you perform the opposite of the calculation above: you divide and subtract instead of multiplying and adding.

Conversion From Decimal to Binary

The easiest of the three conversions from decimal is to binary—since the maximum value of each digit is one, there is no dividing, just subtraction. All you do is the following:

1. Find the largest power of two that is smaller than the number.

2. Put a “1” in the digit place for that power of two and subtract that power of two from the decimal number.

3. Repeat steps #1 and #2 until you are reduced to zero.

This is easier to explain using an example and a table, of course. Let's convert the decimal number 689, as shown in Table 5. Again, read the table starting from the upper left, and going down and then across. We start by noticing that 1024 is not less than or equal to 689, so the “1024s” place gets a 0. In the next place, 512 is less than 689, so we make the “512s” place a 1 and subtract 512 from 689 to leave 177. The calculation continues, eventually showing shows that 689 decimal is 1010110001 binary.

 Decimal Value Before Considering This Digit Place 689 689 177 177 49 49 17 1 1 1 1 Power of Two 210 29 28 27 26 25 24 23 22 21 20 Value of Digit Place 1024 512 256 128 64 32 16 8 4 2 1 Value of Digit Place Equal To or Less Than Current Decimal Number? No Yes No Yes No Yes Yes No No No Yes Subtraction Step skip 689- 512 = 177 skip 177- 128 = 49 skip 49-32 = 17 17-16 = 1 skip skip skip 1-1 = 0 Binary Digits 0 1 0 1 0 1 1 0 0 0 1

Conversion From Decimal to Octal or Hexadecimal

The process for octal and hexadecimal is almost the same, except you must divide by powers of two instead of just subtracting:

1. Start with the highest power of 16 (hexadecimal) or 8 (octal) that is smaller than the number.

2. Divide the decimal number by that power, keeping only the integer part of the result.

3. Keep the remainder after the division is done, for the next step.

4. Repeat steps #1 to #3 until you get to the “ones” place, and then put there whatever is left after the higher digits were done.

Table 6 shows the same example as Table 5 but goes from decimal to hexadecimal instead of decimal to binary: 689 in decimal is 0x2B1 hexadecimal.

 Decimal Value Before Considering This Digit Place 689 689 177 1 Power of 16 163 162 161 160 Value of Digit Place 4096 256 16 1 Value of Digit Place Smaller Than Current Decimal Number? No Yes No n/a Division Step skip 689/256 = 2.691; use “2” for this digit. 177/16 = 11.0625; use “B” for this digit. n/a Remainder After Division skip 177 1 n/a Hexadecimal Digits 0 2 B 1

If you find The TCP/IP Guide useful, please consider making a small Paypal donation to help the site, using one of the buttons below. You can also donate a custom amount using the far right button (not less than \$1 please, or PayPal gets most/all of your money!) In lieu of a larger donation, you may wish to consider purchasing a download license of The TCP/IP Guide. Thanks for your support!
 Donate \$2
 Donate \$5
 Donate \$10
 Donate \$20
 Donate \$30
 Donate: \$