Converting Hexadecimal in your head ======================================== by Riftor http://home.graffiti.net/riftor615/ ======================================== Notes: I recommend viewing this with a fixed size font, so that the ASCII diagrams look ok. If your viewing in a text terminal that's fine, or if your using notepad or something Fixedsys will do :) Intro ----- Hexadecimal, hex (for short) or base-16 (for mathmatcians heh) is a number system that is built using 1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F. It is commonly used with computers, more than anything as a way of saving space as the number 255 would take up 3 chars but FF (255 in hex) is only two heh. In actual storage of data this isn't too much of a problem as its all stored in binary anyway, but when converting out it is a lot easier to have things structured in 2 bytes, especially when dealing with raw hex bytes of a program, seeing 144 instead of 90 (that's the hex of the processor instruction NOP [null operattor, it doesn't do anything] btw heh) would just be annoying when viewing a program listing, in hex all values up to 255 are displayed with 1 or 2 bytes, decimal it can be displayed with 1, 2 or 3 bytes, and computer just don't work with groups of 3 ok!? Also with hex colour values it takes up less space (for example in HTML docs) etc. etc ahem so anyway, this isn't so important, lets get onto the calculation:- Decimal To Hex -------------- It is a lot easier if you know the first 16 or so "numbers" of hex and their decimal equivalents... :- Hex Decimal 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A 10 B 11 C 12 D 13 E 14 F 15 10 16 11 17 and so on....... For my example, lets use the number 615 (heh why not go big? heh) Calculating Hex from a decimal number can easily be done with a few simple steps:- 1). Divide the number by 16. 615/16 = 38 + remainder of 7 lets call 38 P for the moment P=38 If the reaminder is less than (or equal to 9) then we don't need to convert it to hex. This is because the first 9 numbers are the same for hex and decimal. In this case it is less than 9 so we now have our last digit, its 7 ;) 2). If the number we got before [(P) which is equal to 38] is greater than 9 then we need to convert that to hex too 38/16 = 2 + remainder 6 The value of P/16 (38/16) is our first digit (reading left to right now), and the remainder is the second. If we join these with first remainder that we found (7) we get the hexadecimal number:- 267 which if you try out on a calculator you will find is 615 in decimal :) These vague formula applies heh d = N/16/16.... answer = d & r(n-1) & ... & r2 & r1 [& just means join the digits] where:- n is the number of digits N is your original decimal number d is your last digit r is a remainder [NOTE: depending on what number there could be lots of remainders, (n-1 remainders) and lots of /16 that's why i've put ...] Don't worry about the formula if you don't understand it, you don't really need it. You basically need to keep diving your number by 16 until you have it less than 16, then you can just join all the numbers together. I'll do another quick example just to help you out if your struggling Decimal number = 2841 2841 / 16 = 177 remainder 9 177 / 16 = 11 remainder 1 So:- r1 = 9 r2 = 1 d = 11 (which = B remember) Therefore 2841 in decimal = B19 Starting to get the hang of it now? heh Hex To Decimal -------------- This again, is similar method just reverse. Let's take the hexadecimal number 5E8 First we need to multiply the first digit (reading left to right) by 16 so:- (btw for any of you non-programmers * = multiply) 5 * 16 = 80 Remainder = E because E is the next digit (E = 14 remember!) 80 + remainder = 80 + 14 = 94 94 * 16 = 1504 remainder = 8 (last digit) 1504 + remainder = 1504 + 8 = 1512 And that is the answer! 1512 This time its just a case of multiplying by 16 then adding the next digit, then multiplying that by 16 and adding the next digit, until you have run out of digits! This general formula applies:- (n-1)(16N+R) Don't worry about the formula if you don't understand it, you don't really need it. Kinda a nicer formula, where N = current value (starts with original value) n = number of digits R = current remainder digit (or next digit along) So just keep multiplying by 16 then adding the next digit then multiply and so on :) easy peasy :D Easy.... Once you practise a bit. Have fun. (shameless self promotion time...again...) http://home.graffiti.net/riftor615/