Convert Hexadecimal to Decimal - NEWBIE QUESTION, PRO POINTS (500) AVAILABLE

I need a program that converts a hexadecimal (base-16) number into a decimal (base-10) number. The program must take as its input a pointer to a character string which has the hexadecimal number in ascii. It must return a pointer to a character string which has the decimal number in ascii. You cannot use any standard C libraries (e.g. scanf()).

Example:
char* Convert(const char *pHexString)
{
//do some conversion stuff
//return result
}

Please note that I will award 500 points to the _first_ person to answer this question to the spec. The code must be a 'clean' and efficient solution in ANSI C please.

i would first convert the hex number to binary. Not too hard, start with a total of zero,get the first character,
if it's 0 to 9, add it to the total, if it's A to F, add ten to it, then add that to the total. THen shift the total left 4 bits, continue until you run out of characters.

now you have to convert the binary number to decimal. Not too hard, take the number modulo 10, that's your bottom digit. Divide the number by 10. repeat until you have zero. That's it.

0

btocakciCommented:

1-Put the pointer to the starting point of string.
2-Truncating the string as long as it ecountered with NULL.
So that find the length of string.
3-Put the pointer to the starting point of string again.
4-Shift the value of pointer to the right by 4 so that the value will be multiplied by 16.Multiply this by the string_length-1. You know the rightmost digits' power is 0, not 1. Add this value to a variable that was initiliaized by 0.Increment pointer by one to read the next digit.
5-After the pointer encounter with NULL,
6-just take the 10 modul and divide the number by 10 until it hits 0.
7-Put the remainders into the elements of your string.
8-return the string that means simply the pointer.

0

btocakciCommented:

You must say the computer that A means 10, B means 11 so on..

Has anything here helped you? If you have some code you think might nearly be an answer, post it here and we can help out. Let us know how you are doing.

Paul

0

Featured Post

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Friedrich