Hey I need some help to get a asignment done for tomorrow!

I need to create a hex to decimal converter.

EASY right!

Well here is the catch. I can't use visual studios libraries to create is so [
string sHex = S;
int iNumber = int.Parse(sHex, System.Globalization.NumberStyles.HexNumber);
string sInt = iNumber.ToString();
]

is out of the question. I need to create it from scratch.

even more It shall handle hex numbers with the value aspect from decimal 127 to -128 or 7f to -80 in hex.


PLZ give me some advices!

Recommended Answers

All 4 Replies

Use a switch case statement to convert every hexadecimal character to a decimal and add all the decimals together. E.G. hex B becomes 11. Multiply by 16 when needed.

are you sure its 7f to -80 and not 00 to ff? As far as i'm aware you dont have a signed hex so -80 is wrong. I'd double check it, but i think they mean that 00 = -128 and ff = 127 in which case you need to apply the subtraction after you convert the hex.

Here is my response to the last hex-to-decimal thread:

divide out and sum the multiples of powers of 16.

i.e. 0x539 = 5*(16^2) + (3*16^1) + 9*(16^0) = 5*256 + 3*16 + 9*1 = 1280 + 48 + 9 = 1337.

The rightmost (least significant bit, LSB) represents 16^0, or 1, times the value 0-F, and so on, with each position to the left increasing the power of 16. So, from right to left, it's some number 0-15 * n where n is increasing from 0 at the right most.


Here are a couple other threads regarding base conversion:
http://www.daniweb.com/forums/thread238803.html
http://www.daniweb.com/forums/thread238418.html

-128 to 127 is 80 to 7F. Look for explanation here: http://en.wikipedia.org/wiki/Two's_complement

commented: Clear example of two's complement "signed" binary/hex numbers. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.