I'm working on a Hexadecimal <--> Decimal converter, and I'm not sure how I should do the conversion process. I don't want actual code, i'm trying to do that part by myself, I just need the process I should go through.
Thanks for your input.
Printable View
I'm working on a Hexadecimal <--> Decimal converter, and I'm not sure how I should do the conversion process. I don't want actual code, i'm trying to do that part by myself, I just need the process I should go through.
Thanks for your input.
Hmm... well, you know the numbering systems, right?
Some languages, like Visual Basic, might actually have a predefined type to put hex into. Most will probably have libraries to do it for you.
But for hex->dec psuedocode with the hex as a string...
VARIABLEX = 1
TOTAL = 0
Go to rightmost hex digit
Loop to go through each digit
{
VARIABLEY = 0
Evaluate the digit value to 1-16 and Store it in VARIABLEY
Multiply VARIABLEY by (16^VARIABLEX), and put that value into VARIABLEY again
(in other wordsSixteen to the variablex'th power)
VARIABLEX = VARIABLEX +1
Add VARAIABLEY to TOTAL
Go to the digit one left of the current one
}
Display TOTAL.
In other other way around... say you have DEC as the decimal value. You could pretty much use this method for other base notations if you just replace '16' within it, I think.
Divide DEC by 16 until you get a number that is below 16, then round down to the nearest whole number. Let's say DEC is 300. (With short integer values you can do this more simply, but oh well.) Multiply that whole number by 16 as many times as you divided by 16, then subtract that from the total.
Divide 300 by 16 = 18.75
18.75 / 16 = 1.171875
Round to 1.
1 x 16 x 16 (because you divided by 16 twice) = 256
300 - 256 = 44
So you have your first digit, which is 1, and it is in the THIRD digit from the right (because it is a coefficient of 16^2) So your final value is
1?? (in hex) + 44 (in dec)
Repeat.
44 = 16 = 2.75
2 x 16 = 32
44-32 = 12
12? (in hex) + 12 (in dec)
12 cannot be divided by 16, so it fits in the ones place, and 12 is the hex equivalent of C...
Hex value is 12C.
Hello Kezil,
Nice idea :)
I'n not sure how helpful this will be, because the way that I actually figure hexadecimal numbers is different. But here is the ideology behind the conversion of hexadecimal numbers.
Just remember that hexadecimal, is base power 16:
so...lets say we have the hexadecimal number D38B2h , the small "h" at the end just says that this is a hexadecimal number.
Now, it is also important to understand the following:
16^0 = 1 (16 "to the power of" 0)
16^1 = 16
16^2 = 256
16^3 = 4096
16^4 = 65,536
It is also important to understand the following:
Decimal - Hexadecimal Equivalent
0 0h
1 1h
2 2h
3 3h
4 4h
5 5h
6 6h
7 7h
8 8h
9 9h
10 Ah
11 Bh
12 Ch
13 Dh
14 Eh
15 Fh
(Please remember that the lowercase h just means hexadecimal)
With this in mind we will break down D38B2h:
D = 13 * 65,536 (16^4) = 851968
3 = 3 * 4,096 (16^3) = 12288
8 = 8 * 256 (16^2) = 2048
B = 11 * 16 (16^1) = 176
2 = 2 * 1 (16^0) = 2
----------------------------------------------------
866482
From this we find that D38B2h = 866482 !
That is how the conversion of hexadecimal to decimal numbers is done which should also give you an idea of how to convert decimal to hexadecimal :)
If you need more information on the subject, our own AO member MsMittens has written a nice tutorial on the subject of decimal, binary and hexadecimal numbers. It is under the Tutorials Forum or you can click the following:
http://www.antionline.com/showthread...hreadid=124423
Good Luck On You Program :) And I Hope I have not confused you!
I Hope This Helps You! :)