Numeral Systems Conversion : Binary, Octal, Decimal and Hexadecimal

As a computer programmer, you need to know about the different numeral systems. In your computer works, there will be lots of times that you will be using numeral systems like the binary, hexadecimal and sometimes octal as well. Thus, it would be a great idea to know how to convert numbers from one numeral system to the other. The digits that all 4 numeral systems use are shown below :
Decimal | Binary | Hexadecimal | Octal |
012
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
0110
11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 |
012
3 4 5 6 7 8 9 A B C D E F 10 |
012
3 4 5 6 7 10 11 12 13 14 15 16 17 20 |
Convert From Decimal to Other Numeral Systems
To convert a number from decimal to any other numeral system, we follow a standard procedure. We divide the integer part of a number with the base number of the system at which we want the actual conversion to occur. For instance, to convert a number from decimal to binary(base 2), we divide that number with 2. While we make each division, the remainder of the division is the rightmost digit of the resulting number in binary and the result of the division gets redivided with 2 till we reach the division where the result is 0. If the number has a fractional part, this gets multiplied by two. The integer part of the number after the multiplication is held, will be the leftmost digit of our new fractional number in the binary system, right after the comma. The result of the multiplication of 2 with the fractional part of the number gets substracted of the integer part used for the creation of the fractional number in the binary system. This new number gets multiplied with 2 again and then the previous procedure is executed again and again, till the new fractional number becomes 0. If the new fractional number is periodic, we cut and round the resulting number. This may sound a bit confusing, so these are some example conversions :
Convert from decimal to binary Χ(10)->Χ(2)
Integer
45(10)->Χ(2)
Div Quotient Remainder Binary Number (Χ)
45 / 2 22 1 1
22 / 2 11 0 01
11 / 2 5 1 101
5 / 2 2 1 1101
2 / 2 1 0 01101
1 / 2 0 1 101101
45(10)->101101(2)
Fractional Part
0,182(10)->Χ(2)
Div Product Remainder Binary Number (Χ)
0,182 * 2 0,364 0 0,0
0,364 * 2 0,728 0 0,00
0,728 * 2 1,456 1 0,001
0,456 * 2 0,912 0 0,0010
0,912 * 2 1,824 1 0,00101
0,824 * 2 1,648 1 0,001011
0,648 * 2 1,296 1 0,0010111
0,182(10)->0,0010111(2) (After we round and cut the number)
Convert from decimal to octal Χ(10)->Χ(8)
Integer
45(10)->X(8)
Div Quotient Remainder Octal Number (Χ)
45 / 8 5 5 5
5 / 8 0 5 55
45(10)->55(8)
Fractional Part
0,182(10)->Χ(8)
Mul Product Integer Binary Number (Χ)
0,182 * 8 1,456 1 0,1
0,456 * 8 3,648 3 0,13
0,648 * 8 5,184 5 0,135
0,184 * 8 1,472 1 0,1351
0,472 * 8 3,776 3 0,13513
0,776 * 8 6,208 6 0,135136
0,182(10)->0,135136(8) (After we round and cut the number)
Convert from decimal to hexadecimal Χ(10)->Χ(16)
Integer
45(10)->X(16)
Div Quotient Remainder Hex Number (Χ)
45 / 16 2 13 D (Since 13 decimal is D in hexadecimal)
2 / 16 0 2 2D (See the table)
45(10)->2D(16)
Fractional Number
0,182(10)->Χ(16)
Mul Product Integer Binary Number (Χ)
0,182 * 16 2,912 2 0,2
0,912 * 16 14,592 14 0,2Ε
0,592 * 16 9,472 9 0,2Ε9
0,472 * 16 7,552 7 0,2Ε97
0,552 * 16 8,832 8 0,2Ε978
0,832 * 16 13,312 13 0,2Ε978D
0,182(10)->0,2E978D(16) (After we round and cut the number)
2. Convert from other numeral systems to decimal
Now, in order to do the opposite, we have to do some pretty different steps than the previous ones. At first, we count the number of digits that our number to convert consists of starting from 0 and going from right to left when the number is an integer. However, when the number is decimal, then we count the digits of the number right after the comma, starting from left and going to the right, indexing them starting from -1. So, in order to convert a number from one numeral system to decimal, we multiply each digit with a number that has its base on the numeral system that the number is represented at. For instance, for binary that number is 2 and we raise that number to the index that each digit is at. In the end, we add all the resulting numbers and the result is that number in decimal. Take a look at the examples below to understand it better :
Convert from binary to decimal Χ(2)->Χ(10)
101101,0010111(2)->Χ(10)
Index the digits of the number
150413120110,0-10-21-30-41-51-61-7
Multiply each digit
1 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 + 0 * 2-1 + 0 * 2-2 + 1 * 2-3 + 0 * 2-4 + 1 * 2-5 + 1 * 2-6 + 1 * 2-7 =
(β-α = 1/βα)
32 + 0 + 8 + 4 + 0 + 1 + 0 + 0 + 0,125 + 0 + 0,03125 + 0,015625 + 0,007813
= 45,179688(10)
Convert from octal to decimal Χ(8)->Χ(10)
55,135136(8)->Χ(10)
Index the digits of the number
5150,1-13-25-31-43-56-6
We multiply each digit
5 * 81 + 5 * 80 + 1 * 8-1 + 3 * 8-2 + 5 * 8-3 + 1 * 8-4 + 3 * 8-5 + 6 * 8-6 =
40 + 5 + 0,125 + 0,03125 + 0,009766 + 0,000244 + 0,0001 + 0,0000229
= 45,1663829(10)
Convert from hexadecimal to decimal Χ(16)->Χ(10)
2D,2E978D(16)->Χ(10)
Index the digits of the number
21130,2-114-29-37-48-513-6
We multiply each digit
2 * 161 + 13 * 160 + 2 * 16-1 + 14 * 16-2 + 9 * 16-3 + 7 * 16-4 + 8 * 16-5 + 13 * 16-6 =
32 + 13 + 0,125 + 0,0546875 + 0,00219727 + 0,00010681 + 0,00000762 + 0,00000077
= 45,18199997(10)
Notice that the decimal numbers are not actually the same as their primary values. We have some losses. These losses are due to the rounds and cuts done in the previous results.
3. Convert from binary to hexadecimal to octal
As we all now, 23 results to 8 and 24 gives 16. This reality, believe it or not, will help you understand how to convert from binary to octal and hexadecimal and vice versa. As we said before, 23 gives 8, so in order to convert a binary number to octal, we need 3 digits that will represent a digit of the octal number. Therefore, for a hexadecimal number we need 4 digits to represent a hexadecimal digit. For that reason, we convert a binary number to octal, then separate the binary number into triads or quadruples (when hex), starting from right to left when talking about integers and from left to right when it’s about decimals starting from the comma. If there are digits left in the end that do not make a triple or a quadruple, for the hex system we pad with 0 in front of the number. Then, we substitute each three digit number or four digit binary number with a digit that corresponds to octal when it’s triples or hex when its quadruples. In this conversion, you will be helped from the table above. Here are some examples to understand what was said :
Convert from binary to octal
110101000,101010(2)->X(8)
| 3 | | 3 | | 3 | | 3 | | 3 |
110 101 000 ,101 010
|| || || || ||
\/ \/ \/ \/ \/
6 5 0 , 5 2 (See that in the array 110(2) corresponds to 6(8) )
110101000,101010(2)->650,52(8)
Convert from binary to hexadecimal
110101000,101010(2)->X(16)
| 4 | | 4 | | 4 | | 4 | | 4 |
0001 1010 1000 ,1010 1000
|| || || || ||
\/ \/ \/ \/ \/
1 Α 8 , Α 8
110101000(2)->1Α8,Α8(16)
4. Convert from hexadecimal to octal and binary
This is the exact reverse procedure than the previous one. As previously, each digit of the octal number corresponds to 3 digits of the binary number and 4 digits of the hexadecimal number. When the appropriate triples or quadruples are not completed, we pad with 0 in front of each number :
Convert from octal to binary
650,52(8)->X(2)
6 5 0 , 5 2
|| || || || ||
\/ \/ \/ \/ \/
110 101 000 , 101 010
650,52(8)->110101000,101010(2)
Convert from hexadecimal to binary
1Α8,Α8(16)->X(2)
1 Α 8 , Α 8
|| || || || ||
\/ \/ \/ \/ \/
0001 1010 1000 ,1010 1000
This is a guest post by a good personal friend of mine under the name Black Shadow.
Hmm..I dont know about these and didn’t tried to know.They had 0’s and 1’s repeating..BUt you made to look easier.
woah.. thanks a lot for this.. ^_^
Hello could I use a few of the insight located on this web site if I produce a website link again to your internet site?
Of course, feel absolutely free to do so.
maybe this is posted for a long time, but it really helped me. thanks a lot^^
@Hiro : You are welcome !
it’s wonderful to read!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
this is much more easier to read and understand than any other guides.. i hope this will help to my exam tommorrow 😀
This makes the number system conversion easier for me to explain to my students. Thankk you very much
Its really excellant and understandable
If not for your writing this topic could be very couoelntvd and oblique.
Thanks A lot
its true but can any body tel me the conversion of floating decimal values into binary or octal or hexadecimal?????????????????????????
Thanks a lot for this valuable post! I have my tomorrow and Till now I was not able to understand this from any book. God bless you mate
@Shiva Singh : I’m glad it helped you, you are welcome
Thanks a lot for this valuable post!
i hope this will help to my exam tommorrow:D
hey ….ds is awsum yar…i never get good marks in number system..but from this i learned number conversion so easily..i love this..god bless u ..n give lotss of knowledge..so that u can share with uss….he he he …m lovin it…:):):)
3. Convert from binary to hexadecimal and octal
NOT
3. Convert from binary to hexadecimal to octal
ALSO
4. Convert from hexadecimal and octal to binary
NOT
4. Convert from hexadecimal to octal and binary
Sorry
4. Convert from binary to hexadecimal and octal
NOT
4. Convert from hexadecimal to octal and binary
please send me:- A3.8(base 16)=?(base 8)
lot of thanks for this . my studies is getting easier
Nice article. Here’s another useful one, which is focues on the implemenetation and provides C# source code: http://www.pvladov.com/2012/05/decimal-to-arbitrary-numeral-system.html
thanks for this ., my study become easier. and my holiday home work is completed.
Nice article. This is basic need to understand the relation between the different number system. How we can convert this number system to other number system.
You can learn programming version of number conversion here.
what is the decimal conversion of this binary number- 0b10010100
It is very very easy to understand from any book or other guide
i think there’s a mistake in converting 5hex to binary, it’s supposed to be 101 not 100, pls confirm
@nestark, you must mean the 110 in the octal to hex conversion right ? It was a mistake indeed, fixed it, thanx
THANKS !!
Thank You for this…..it really help me.
how can explain this…the decimal=320+binary=10111011+octal=760+hexadecimal=AC0=________16?
2) decimal=1500+hexadecimal=800+binary=1111100+octal= ? =hexadecimal=4,096
3) decimal=(2000/5+hexadecimal=182+decimal=416+binary=11100=_________8
hope you can help me…please =)
i hope you can help me to answer and explain this..
320(base10)+10111011(base2)+760(base8)+AC0(base16)=__________(base16)?
1500(base10)+800(base16)+1111100(base2)+_________(base8)?=4,096(base16)
(2000/5(base10)+182(base16)+416(base10)+11100(base2)=_____________(base8)?
please..i will wait for your reply..thanks a lot
Thanks a lot…
thank you! I’m every difficult study this code but now is Ok when i have follow up this website…
sorry but i m not able 2 understand anything……..
THANK YOU SO MUCH………!!!!!!!!!!!!BUT STILL I NEED SOME PROBLEMS TO SOLVE……..!!!!!!!!!THATS OK…….!!!
Thanksssssss for the author. This was a very big help! More Power and God Bless you. =)
I’m really thankful to person who posted this and this was really helpful in my exam!!!!!!
simply you are awesome boss
wow sir,what a great steps to understand, truely respect you people!
very helpful information related to number system
making great future of mine awesome website
thank,s Fawad sir .you are great teacher……..
it’s realy very easy to under stand
How about converting decimal into hexadecimal?
@Batahi Mosha : It’s there, you missed it, almost mid post.
I realy learned the base of convesion process. thanks a lot for such a good teaching.
how about conveting binary to integral
its realy helping but i need to solve some questions given in home work .. :/ help me plz
really its easy to understand and easy to learn …
Even if it is good point
their is something that is needed in order to increase the knowledge for those student
i like the way that presented to us thank you
good job man!!!!!!!!
very helpful for serlf studyyyyyyyyyyyyy…………
thnx alot …it really helped me
thank you very much.Now l can able to do any conversion.
Ιnformative article, totally what I needed.
WOW just what I was looking for. Came here by searching for learn the numeral systems
Hi there, this weekend is good in support of me, because this point
in time i am reading this impressive educational piece of
writing here at my house.
The i – Phone has quickly become the mot adorable phone in the planet andd wifh the newer versions
of the phone coming up every noww and then the sales of the device have really skyrocketed.
Skype is by far the choicest mobile voice and videso calling
app around which connects you to another Skype subscriber on the i – Phone, Android,
Mac, or PC from your phone’s Wi-Fi. It enhances your decision-making comjpetency annd opens the line of communication making it irresistible for children and the kids-at-heart.