Solved

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

Posted on 2006-06-02
4,082 Views
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.
0
Question by:thegilb

LVL 24

Expert Comment

Friedrich
0

LVL 53

Expert Comment

Check out fscanf and fprintf :

http://www.cplusplus.com/ref/cstdio/fscanf.html
http://www.cplusplus.com/ref/cstdio/fprintf.html

pay specific attention to the format modifiers ...

And as fridom said, show us what you've got, and we'll help you along if needed.
0

Expert Comment

i think you must convert the number to a string ,at least, so that your return value become a string.
0

LVL 22

Assisted Solution

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

Accepted Solution

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

Expert Comment

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

LVL 16

Expert Comment

Hi thegilb,

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!