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):

#include <stdio.h>
 int main()

 {
       int hexa;

      scanf("%x", &hexa);
      printf("%d", hexa);

        return 0;

 }

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

> 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 :)

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

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>

Yes I surely added, I actually directly tried the example code in the cplusplus page you sent...

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...

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

This question has already been answered. Start a new discussion instead.