05-28-2011, 03:19 AM
A few days ago i learnt how to manually convert Hexidecimal numbers into Binary, Binary to Decimal numbers and Decimal to Hexidecimal. I thought i'd share it because it is quite easy and most people do not know how to do it.
Let's start with the basics;
Decimal numbers are Base 10 numbers, that is to say that each number is to the power of 10, although the power is incremented by 1 as you progress to each individual character in the number. (Confused? Prepare for the example)
Decimal numbers: 0,1,2,3,4,5,6,7,8,9
Example - Expanded:
183 is read in decimal by humans as 183, simple right. Well this is how the computer reads it:
1 - 8 - 3, Each Character is seperated and multiplied by 10^(Char Index). 1 has the Character Index of 2, 8 has the Character index of 1 and 3 has the Charcter index of 0. (The character index is read from right to left)
So when 183 is entered, the first number, 3 (which is read right to left, not left to right) is multiplied by 10^0 = 1. (i.e. 3x1 = 3) then the 8 is multiplied by 10^1 = 10. (i.e. 8x10 = 80) then lastly 1 is multiplied by 10^2 = 100. (i.e. 100x1 = 100). Now all you have to do is add them together, 100 + 80 + 3 = 183.
Now you're probably thinking, well holy crap. All that just to tell me what i know already, 183 = 183. Well, in actual fact, yes i just did but i've also shown you the basics of the Base function and how to multiply numbers with their respective bases.
Hexidecimal Numbers: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
As you probably realise is that it's just the decimal number system but with a few letters thrown in. True, but these letters have Values and to keep it simple, here they are:
A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15
Note: To seperate the Hexidecimal number (13) from a Decimal or Octal Number each number system has their own seperation symbol(s) (i.e. &H for Hexidecimal, 0x (Zero - x) for Octal(Will not cover this, Sorry.) and decimal is left as is otherwise the whole population of Earth will be learning a new counting system.
So Decimal 13 would equal Hexidecimal &HD. Quite obvious from above, but let's dive into the harder numbers.
Example - Expanded:
Now try to convert the decimal number (183) into Hexidecimal, if it's providing too hard of a challenge, have a look at the answer below:
Let's seperate the number(s) and prepare it for conversion:
Firstly we need to divide 183 by 16 (base number).
183 / 16 = 11.475
Now with Hexidecimal, you don't want to round off to a stupid decimal point. So we will just use 11 for now.
So what's 11 good for? Well 11 x 16 = 176 (Most of our Decimal number! :tongue: )
Now that we know 11x16 = 176, what is the other 7 going to become?
And the answer is *drumroll* 7.
That's right, 176 + 7 = 183. Confusing of how that is needed for Hexidecimal right? Well here comes the finale.
By now you're thinking, sweet i've got this, the answer for Hex 11 is &H11 = 176, but you're wrong.
&H11 is not a Hexidecimal number, you may think it will work but, this will just become 1x16^1 + 1x16^0 = 17. So we use the extra letters thrown into the Number System (A-F) and find the letter that equals 11 or B and swap them so it now becomes:
&HB instead of &H11 which solves our 16x1^1 problem and turns it into the solution for B(11)x16^1 = 176.
That ignorant 7 isn't left alone floating, it's just thrown on the end, why? Well because 7x16^0 = 7. (Try it, it's right) and because 176+7 = 183, &HB7 = 11x16^1 + 7x16^0 = 183.
Answer in Hexidecimal: &HB7
And now for binary, using knowledge gained from the Hexidecimal and Decimal sections above, it is simple to figure out that Binary is a Base 2 number system as it only consists of 0's and 1's.
Note: Not all numbers are not of an 8 bit standard, you can go from a 1 bit to an 8 bit number, but they have exclusions as i will later note.
Ok, let's get started:
Back to the good ol' 183 Decimal Number, &HB7 Hexidecimal and time for the 183 Binary Conversion.
Now, this should be easy for most of you as you have gone from Decimal to Hexidecimal and now finally to Binary. (By the way, you can go from any number system to any number system (i.e Hex to Bin, Bin to Dec or Dec to Hex etc).
First off, let's get to the Base 8 equations, hoping that most of you know you're 8x8 tables we can assume that the Index of 0 = 1 (As it always will) and that Index 1 = 2, 3 = 4, 4 = 16 etc etc.
Now because Binary only uses 0's and 1's, when you multiply a 1 bit in the Index(3) of lets say a 5 bit number (01000) then it won't become 01600, because that's impossible in terms of binary.
So, without looking at the conversion the same as Hex and Dec, we need to look at it like simple math. Which it is.
Lets start with a 4 bit number, 0101 for example and convert it to Dec.
Ok, now we have a character array, per say, of the 4 bit number and need to find out what each 0 and 1 represents, here we go:
Firstly, show each index and their respective base numbers to the power of the index. (i.e. Index(3) if it were a 1 bit would equal 1x2^3 = 8, in a 5 bit number system 01000 would equal 8)
Now to simplify this even further, here is an easy way to get the decimal representation of a binary number:
The first index (0) is always equal to 1, the second is equal to 2 then 4 then 8 then 16 etc (as shown above), so the value of a bit in each index is always a constant.
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 is the value for an 8 bit number (11) which equals 127. The 8 bit binary number 01100101 would equal 64 + 32 + 4 + 1 = 101.
Exclusions on bit lengths: I stated earlier that most 1-8 bit numbers have exclusions. These exclusions are that when you have, lets say a 4 bit number and you wanted to make Dec 16 from a Bin 4 bit number you would presume that 8+4+2+1 are values within a 4 bit number all add up to a maximum possability of 15, irreversibly denying the request to make Dec 16 from a Bin 4 bit number.
Now that brief explanation should have hopefully either made you more confused than you were when you started or cleared everything up and agev you the ability to convert Dec to Hex, Hex to Bin and Bin to Dec.
For more Information on Binary and how it works look around for:
One's Complement
Two's Complement
Signed Bits
Unsigned Bits
And that about sums manually converting the 3 types of number systems widely used in Programming.
Hope you enjoyed the read. mile:
Let's start with the basics;
Decimal Numbers
Decimal numbers are Base 10 numbers, that is to say that each number is to the power of 10, although the power is incremented by 1 as you progress to each individual character in the number. (Confused? Prepare for the example)
Decimal numbers: 0,1,2,3,4,5,6,7,8,9
Example - Expanded:
183 is read in decimal by humans as 183, simple right. Well this is how the computer reads it:
1 - 8 - 3, Each Character is seperated and multiplied by 10^(Char Index). 1 has the Character Index of 2, 8 has the Character index of 1 and 3 has the Charcter index of 0. (The character index is read from right to left)
So when 183 is entered, the first number, 3 (which is read right to left, not left to right) is multiplied by 10^0 = 1. (i.e. 3x1 = 3) then the 8 is multiplied by 10^1 = 10. (i.e. 8x10 = 80) then lastly 1 is multiplied by 10^2 = 100. (i.e. 100x1 = 100). Now all you have to do is add them together, 100 + 80 + 3 = 183.
Now you're probably thinking, well holy crap. All that just to tell me what i know already, 183 = 183. Well, in actual fact, yes i just did but i've also shown you the basics of the Base function and how to multiply numbers with their respective bases.
Hexidecimal Numbers
Hexidecimal numbers are Base 16 numbers, now take what you've learned previously and apply it to Hexidecimal numbers but instead of base 10, you change it to base 16. Work with the same number as before (183) and try to manually change it into a Hexidecimal number, but before you do that you need to know a few things, such as the Hexidecimal number system.Hexidecimal Numbers: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
As you probably realise is that it's just the decimal number system but with a few letters thrown in. True, but these letters have Values and to keep it simple, here they are:
A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15
Note: To seperate the Hexidecimal number (13) from a Decimal or Octal Number each number system has their own seperation symbol(s) (i.e. &H for Hexidecimal, 0x (Zero - x) for Octal(Will not cover this, Sorry.) and decimal is left as is otherwise the whole population of Earth will be learning a new counting system.
So Decimal 13 would equal Hexidecimal &HD. Quite obvious from above, but let's dive into the harder numbers.
Example - Expanded:
Now try to convert the decimal number (183) into Hexidecimal, if it's providing too hard of a challenge, have a look at the answer below:
Spoiler (Click to View)
Let's seperate the number(s) and prepare it for conversion:
Firstly we need to divide 183 by 16 (base number).
183 / 16 = 11.475
Now with Hexidecimal, you don't want to round off to a stupid decimal point. So we will just use 11 for now.
So what's 11 good for? Well 11 x 16 = 176 (Most of our Decimal number! :tongue: )
Now that we know 11x16 = 176, what is the other 7 going to become?
And the answer is *drumroll* 7.
That's right, 176 + 7 = 183. Confusing of how that is needed for Hexidecimal right? Well here comes the finale.
By now you're thinking, sweet i've got this, the answer for Hex 11 is &H11 = 176, but you're wrong.
&H11 is not a Hexidecimal number, you may think it will work but, this will just become 1x16^1 + 1x16^0 = 17. So we use the extra letters thrown into the Number System (A-F) and find the letter that equals 11 or B and swap them so it now becomes:
&HB instead of &H11 which solves our 16x1^1 problem and turns it into the solution for B(11)x16^1 = 176.
That ignorant 7 isn't left alone floating, it's just thrown on the end, why? Well because 7x16^0 = 7. (Try it, it's right) and because 176+7 = 183, &HB7 = 11x16^1 + 7x16^0 = 183.
Answer in Hexidecimal: &HB7
Binary Numbers
Notice how each number system has a base number and each base number is the exact number of characters in the number system. i.e. Decimal is a base 10 number system with 10 Characters (0-9), whilst Hexidecimal is a base 16 number system with 16 Characters (0-F).And now for binary, using knowledge gained from the Hexidecimal and Decimal sections above, it is simple to figure out that Binary is a Base 2 number system as it only consists of 0's and 1's.
Note: Not all numbers are not of an 8 bit standard, you can go from a 1 bit to an 8 bit number, but they have exclusions as i will later note.
Ok, let's get started:
Back to the good ol' 183 Decimal Number, &HB7 Hexidecimal and time for the 183 Binary Conversion.
Now, this should be easy for most of you as you have gone from Decimal to Hexidecimal and now finally to Binary. (By the way, you can go from any number system to any number system (i.e Hex to Bin, Bin to Dec or Dec to Hex etc).
First off, let's get to the Base 8 equations, hoping that most of you know you're 8x8 tables we can assume that the Index of 0 = 1 (As it always will) and that Index 1 = 2, 3 = 4, 4 = 16 etc etc.
Now because Binary only uses 0's and 1's, when you multiply a 1 bit in the Index(3) of lets say a 5 bit number (01000) then it won't become 01600, because that's impossible in terms of binary.
So, without looking at the conversion the same as Hex and Dec, we need to look at it like simple math. Which it is.
Lets start with a 4 bit number, 0101 for example and convert it to Dec.
Ok, now we have a character array, per say, of the 4 bit number and need to find out what each 0 and 1 represents, here we go:
Firstly, show each index and their respective base numbers to the power of the index. (i.e. Index(3) if it were a 1 bit would equal 1x2^3 = 8, in a 5 bit number system 01000 would equal 8)
Now to simplify this even further, here is an easy way to get the decimal representation of a binary number:
The first index (0) is always equal to 1, the second is equal to 2 then 4 then 8 then 16 etc (as shown above), so the value of a bit in each index is always a constant.
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 is the value for an 8 bit number (11) which equals 127. The 8 bit binary number 01100101 would equal 64 + 32 + 4 + 1 = 101.
Exclusions on bit lengths: I stated earlier that most 1-8 bit numbers have exclusions. These exclusions are that when you have, lets say a 4 bit number and you wanted to make Dec 16 from a Bin 4 bit number you would presume that 8+4+2+1 are values within a 4 bit number all add up to a maximum possability of 15, irreversibly denying the request to make Dec 16 from a Bin 4 bit number.
Now that brief explanation should have hopefully either made you more confused than you were when you started or cleared everything up and agev you the ability to convert Dec to Hex, Hex to Bin and Bin to Dec.
For more Information on Binary and how it works look around for:
One's Complement
Two's Complement
Signed Bits
Unsigned Bits
And that about sums manually converting the 3 types of number systems widely used in Programming.
Hope you enjoyed the read. mile: