A Lesson In HEXadecimal ----------------------- By: Sea4 of the CodeBreakers It has come to my attention that some virus authors, people who code and people who just know something about computers don't understand what hexadecimal is. They acknowledge that a Hex ( short for Hexadecimal ) number holds some value, but what exactly that value is beyond them. This article hopes to teach what hexadecimal IS, how it WORKS, and how to CONVERT between Hex and the normally used Decimal system. What's is Hexadecimal? --------------------- Hexadecimal, or Hex as we'll refer to it, is what they call a number system. To represent any number you must use a number system, they can be referred to as bases. We use the Decimal system most often, also called Base-10. Hexadecimal is know as Base-16, Binary is Base-2, and Octal is Base-8. The Base system is named after the number of characters that can be used to represent a number of that system. In the decimal system ( Base-10 ) we use ten characters... 0 1 2 3 4 5 6 7 8 and 9. Any number that we want to show in decimal must use a series of those 10 number characters, and those characters only. But with only a limited number of characters, how can we represent large numbers? To represent a number larger than 9, we use PLACES. Each place holds a certain value, for example, the number.... 45,235 That number has five places. Each place is a power of 10 ( 10^x ) greater than the place to its right. We can represent numbers by the sum of their places... like this 45,235 = 40,000 + 5,000 + 200 + 30 + 5 OR 45,235 = 4*10^4 + 4*10^3 + 2*10^2 + 3*10^1 + 5*10^0 You'll notice that each place is a power of ten greater than the place before it. This is only true in Decimal. The place values correspond directly to the number system we are using. Since this is Base-10, each place is a power of 10. Thus, in Hexadecimal ( Base-16 ), each place holds a value to the power of 16. So, places are important when representing a number in any base, but as we mentioned before, every base is named after the amount of characters it uses to represent its value. With Hexadecimal ( Base-16 ), we are gonna need sixteen characters, so we use 0 1 2 3 4 5 6 7 8 9 A B C D E and F. Because there are only 10 number characters, we use letters to represent the additional values in Base-16. Confusing at first, its really quite simple, each letter directly corresponds to a value... A = 10 B = 11 C = 12 D = 13 E = 14 F = 15 The only thing is that now... because we are in Base-16, we can use these characters to represent larger values, while only occupying a single space in the sequence. How does it work? ----------------- It works the same way the decimal system does, a number sequence uses any of the possible values ( 0 - F ), and the place of each character determines the true value of the hex number as a whole. Lets try and figure one out. ** For ease, every Hex # will be followed by a lower-case 'h' and every decimal # will be followed by a 'd' ** D12h = D00h + 10h + 2h OR D12h = D*16^2 + 1*16^1 + 2*16^0 As you'll notice, the first line shows D12h equals the sum of its places, and the second line shows it as a sum of its characters, multiplied by a power of 16. This is basically the same thing we did for Decimal, and brings to light a very simple formula for figuring out any number, in any base system. ( # Character ) * ( ( Base number ) ^ Place - 1 ) # Character: Any one of the characters used in that base system. In Hex this would be ( 0 - F ). Base Number: If you are in decimal, ( Base-10 ) it would be 10. Very simple to understand. Place: This is where the number is in the sequence. In the number 10d, the '1' character is in the second place, or the 2 place. Lets try and use it on a series of numbers from different base systems... Octal ( Base-8 ): 12536 = 1*8^4 + 2*8^3 + 5*8^2 + 3*8^1 + 6*8^0 Binary ( Base-2 ): 0101011 = 0*2^6 + 1*2^5 + 0*2^4 + 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0 Base-3: 12001 = 1*3^4 + 2*3^3 + 0*3^2 + 0*3^1 + 0*3^0 Those examples were just so you know how the formula works. Its used to convert numbers into decimal from the other systems. How does one convert Hex to Decimal? ------------------------------------ That's very simple, just use the formula: ( # Character ) * ( ( Base number ) ^ Place - 1 ) Lets give it a try with some numbers we might come across. 19A2:0100 02 E8 9E F8 EB 68 8B 76-0C 8B DF 8B 44 0A 8B CF .....h.v....D... 19A2:0110 C1 E3 02 C4 3E 50 56 26-29 01 8B 46 34 00 91 19 ....>PV&)..F4... 19A2:0120 1E 50 56 26 01 41 02 FF-76 FC 8B 5E FA 53 8B 56 .PV&.A..v..^.S.V That's a little section of output from DEBUG.exe, let's gab the first 3 numbers it got and convert em. 02 E8 9E, First we'll do em separately and then as on big number. This one is easy. 02h = 0*16^1 + 2*16^0 ; Any number to the 0 power = 1 = 0*16 + 2*1 = 0 + 2 --------------------- = 2d A bit tougher. E8h = E*16^1 + 8*16^0 ; Remember, E = 14 = 14*16 + 8*1 = 224 + 8 --------------------- = 232d And the last one. 9Eh = 9*16^1 + E*16^0 = 9*16 + 14*1 = 144 + 14 --------------------- = 158d Now, lets put em all together. 02E89Eh = 2*16^4 + E*16^3 + 8*16^2 + 9*16^1 + E*16^0 = 2*65536 + 14*4096 + 8*256 + 9*16 + 14*1 = 131072 + 57344 + 2048 + 144 + 14 ------------------------------------------------------ = 190622d I know it looks like a lot, but I doubt you'll ever need to do a Hex number of 4 or more places with a pencil and paper. But it helps to understand the numbers you are dealing with. Try and make hex numbers of your own by random, then try and convert them to decimal, you can check your answers with Windows calculator.