You are on page 1of 4

Hex -> Binary

For this one, we're just going to follow the reverse procedure. Take each hex digit, find it's decimal equivalent, and then convert that small decimal number to a four-bit binary: Let's try 0x4B9: 9 in hex is also 9 in decimal. Following the procedure in the decimal->binary description, this comes out to 1001b B in hex is 11 in decimal. 11 decimal comes out to 1011b 4 in hex is 4 in decimal. 4 decimal comes out to 0100b Put all those blocks together: 0x4B9 = 010010111001

Hex -> Decimal


You have two choices here. The first, again involves knowing your powers of 16. For that, you'd take each hex digit, and multiply its decimal equivalent by the appropriate power of 16, then sum the results. So our example above: 0x4B9 would come out to: (9 x 1) + (11 x 16) + (4 x 256) As you can see, those are non-trivial computations. Instead, I rather strongly recommend you simply convert from Hex to Binary first, as above, and then follow the Binary->Decimal conversion algorithm to find your final result. Doing that, we find that 0100 1011 1001b comes out to: 1 + 8 + 16 + 32 + 128 + 1024 = 1209

To manually do base-10 to base-n conversions, go through the following steps: 1. Take your original base-10 number and divide it by your new base, n (2 or 16 for your examples, but it works with any base). 2. Prepend the remainder to your new number. If you havent been through this step before, then this becomes your least significant digit. 3. If the quotient is greater than the base, go back to step 1, using the new quotient instead of the original number. Otherwise, continue to step 4. 4. Append the quotient to the beginning of the new number. For example, lets say your original base-10 number is 48879, and you want to convert to base-16. You would go through this process: 1. 48879 / 16 => quotient 3054, remainder 15 (written as "F"). 2. New number is F. 3. 3054 > 16, so go back to step 1. 1. 3054 / 16 => quotient 190, remainder 14 ("E"). 2. New number is EF. 3. 190 > 16, so go back to step 1.

Numbering system

1. 190 / 16 => quotient 11, remainder 14 ("E"). 2. New number is EEF. 3. 11 < 16, so go on to step 4. 4. 11 is written as "B", so new number is BEEF. Mmm, beefy. For base-10 to base-2 (binary) specifically, this formula can be simplified a bit: 1. If your original base-10 number is odd, then prepend a "1" to your new number, otherwise prepend a "0". 2. Divide your base-10 number by two, and throw away the remainder (you already handled it). 3. If you are left with 1, prepend a "1" to your new number, and youre done. Otherwise, go back to step 1. For example, lets say your original decimal number is 201, and you want to convert it to binary. 1. 201 is odd, so new number is 1. 2. 201/2 is 100.5, so use 100. 3. Go back to step 1. (Ill omit this step from the listings until were left with one.) 1. 100 is even, so new number is 01. 2. 100/2 is 50. 1. 50 is even, so new number is 001. 2. 50/2 is 25. 1. 25 is odd, so new number is 1001. 2. 25/2 is 12.5, so use 12. 1. 12 is even, so new number is 01001. 2. 12/2 is 6. 1. 6 is even, so new number is 001001. 2. 6/2 is 3. 1. 3 is odd, so new number is 1001001. 2. 3/2 is 1.5, so use 1. 3. Since were left with 1, the new number is 11001001, and were done. Now, to convert from base-n to base-10, we follow a little different procedure. Actually, there are two ways to approach this, and you can use whichever makes the most sense to you. 1. Take the last digit of your base-n number, and multiply it by n. This becomes your initial new number. 2. While theres another digit, multiply the previous digit by n to the next higher power, then add that to your new number. For example, to convert the hexadecimal 121DD to decimal: 1. Last digit is D (13); 13*16 = 13, so new number is 13. 2. Previous digit is D; 13*16 = 208, so new number is 13+208 = 221. 2. Previous digit is 1; 1*16 = 256, so new number is 221+256 = 477. 2. Previous digit is 2; 2*16 = 8192, so new number is 477+8192 = 8669. 2. Previous digit is 1; 1*16^4 = 65536, so new number is 8669+65536 = 74205. 2. No more digits, so were done. The other way is to start at the beginning of the number:

Numbering system

1. Take the first digit of your base-n number. This becomes your initial new number. 2. While theres another digit, multiply your original number by n, then add the next digit to it. Using the same example: 1. First digit is 1, so new number is 1. 2. Next digit is 2, so new number is (16*1)+2 = 18. 2. Next digit is 1, so new number is (16*18)+1 = 289. 2. Next digit is D, so new number is (16*289)+13 = 4637. 2. Next digit is D, so new number is (16*4637)+13 = 72405. No more digits, so were done. Now, converting between decimal and hexadecimal directly is actually quite a bit easier than using decimal as an intermediate step, since there is a direct conversion between the digits. Hexadecimal Binary Digits 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 1010 B 1011 C 1100 D 1101 E 1110 F 1111 If you have the hexadecimal number FACE, you can convert it to binary by directly substituting the corresponding values: F=1111, A=1010, C=1100, E=1110, so FACE=1111101011001110. Converting from binary to hex is only very slightly more complicated. Starting from the last digit, separate the digits into blocks of four. You may not complete with a full set, but you can prepend zeroes to make it a full four. For example, if you have the binary number 11010010001000001000000001, you start at the end and work your way backwards, inserting spaces every fourth digit: 11 0100 1000 1000 0010 0000

Numbering system

0001. Since your last set only has two digits (11), you prepend two zeroes to make four digits (0011). Then just substitute as above, which gives you 3488201 in hex.

Conversion between binary and hex is simply regrouping; conversion to/from decimal shows the general process
Conversion from hex to binary is quite easy. Make yourself a table going from 0 to 15 with one column decimal, next column binary, third column hex. For example, the eleventh row will be: 10 1010 a In the second column, it will help to add leading zeros so that each number is expressed as 4 bits, for example write 5 as 0101. Let's say the hex number is a59. Replace the digit a by its binary equivalent, 1010. Replace 5 by 0101. Replace 9 by 1001. String them together: 1010 0101 1001 and take out the spaces, that's the binary number. Binary to hex is just as easy. Group the binary digits in fours, replace each group of four using your table. Decimal to hex: 1. Divide decimal number by 16 and find remainder; use table to find hex digit. example: decimal number is 100. 100/16 = 6 remainder 4. 2. Take dividend, divide by 16 if necessay, find remainder, make is a hex digit. example: 6/16 = 0 remainder 6. 3. Continue until dividend is 0. 4. String the remainders together: 100 decimal = 64 hex Decimal to binary: Same idea, but divide by 2 each time and collect remainders. Binary to decimal: Make yourself a table of powers of 2. (You need to know these like you know your decimal multiplication facts.) Look up each place value 1 of the binary number, add the results. Example: 10110 = 16 + 4 + 2 = 22

Numbering system

You might also like