hexadecimal to decimal

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 14
Reputation: newbiecoder is an unknown quantity at this point 
Solved Threads: 0
newbiecoder newbiecoder is offline Offline
Newbie Poster

hexadecimal to decimal

 
0
  #1
Apr 26th, 2009
Hi I want to write a program which reads a hexadecimal number and returns an decimal number.

I wrote that code(inspired by some code pieces from web):

  1. #include <stdio.h>
  2. int main()
  3.  
  4. {
  5. int hexa;
  6.  
  7. scanf("%x", &hexa);
  8. printf("%d", hexa);
  9.  
  10. return 0;
  11.  
  12. }

but it does't give the answer I want

I want to get 16 for 10000000 or 1109651713 for 01F12342 .

Could you please help me with that?

Thanks
--
newbiecoder
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: hexadecimal to decimal

 
0
  #2
Apr 26th, 2009
> Use the itoa function / sprintf function if you want to convert decimal to hexadecimal (I'm not sure about the fact that the sprintf function will convert a string containing a decimal number to a hexadecimal number, you can simply test it if you want to know it) ...

> If you don't care about the fact the code is in C/C++ you can also check out this code snippet
Last edited by tux4life; Apr 26th, 2009 at 7:39 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: newbiecoder is an unknown quantity at this point 
Solved Threads: 0
newbiecoder newbiecoder is offline Offline
Newbie Poster

Re: hexadecimal to decimal

 
0
  #3
Apr 26th, 2009
Although itoa function seems very useful, I couldn't use it, I use Ubuntu and gcc compiler. And when I try to compile I get this error:
undefined reference to `itoa'
What is the problem you think?
Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: hexadecimal to decimal

 
0
  #4
Apr 26th, 2009
Originally Posted by newbiecoder View Post
Although itoa function seems very useful, I couldn't use it, I use Ubuntu and gcc compiler. And when I try to compile I get this error:


What is the problem you think?
Thanks
Did you add the following include directive at the top of your code?
#include <stdlib.h>
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: newbiecoder is an unknown quantity at this point 
Solved Threads: 0
newbiecoder newbiecoder is offline Offline
Newbie Poster

Re: hexadecimal to decimal

 
0
  #5
Apr 26th, 2009
Yes I surely added, I actually directly tried the example code in the cplusplus page you sent...
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: newbiecoder is an unknown quantity at this point 
Solved Threads: 0
newbiecoder newbiecoder is offline Offline
Newbie Poster

Re: hexadecimal to decimal

 
0
  #6
Apr 26th, 2009
And I tried the code in another compiler. It worked but as I see it gets numbers in decimal but I need to get numbers in hexadecimal...
Thanks...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: hexadecimal to decimal

 
0
  #7
Apr 26th, 2009
1: Zero your Total
2: Read the input as a string (hex)
3: Check each character and conve rt it into decimal value (A=10, B=11, etc)
4: Multiply Total by 16 and add the above value
5: Back to 2
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1655 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC