[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4086
  • Last Modified:

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.
0
thegilb
Asked:
thegilb
2 Solutions
 
fridomCommented:
where's your try?

Friedrich
0
 
Infinity08Commented:
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
 
btocakciCommented:
i think you must convert the number to a string ,at least, so that your return value become a string.
0
Independent Software Vendors: We Want Your Opinion

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!

 
grg99Commented:
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..
0
 
PaulCaswellCommented:
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

Industry Leaders: We Want Your Opinion!

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now