Convert Hex to decimal in C/C++?

Can anyone provide me with a function that would convert a hex value to decimal in C/C++?

The following function converts from decimal to Hex:
string outputashex(unsigned long l) {
     char buffer[1024];
     itoa(l, buffer, 16);
     return buffer;
}

I need from hex to decimal.
Thanks a lot.

Ahmed
abayyariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
  #include <stdlib.h>
            unsigned long strtoul(const char *S, char **PTR,
                int BASE);

            unsigned long _strtoul_r(void *REENT, const char *S,
                char **PTR, int BASE);

for more information see manual pageof strtoul
0
Tommy BraasCommented:
Try this;

#include <stdlib.h>

unsigned long inputfromhex(const char *str)
{
    return strtoul(str, NULL, 16);
}


If your string contains things other that a hexadecimal number, like "ad23Hi How are you?", you would want to pass in a pointer to get the position following the last digit in the number being converted from that string. The pointer would in the above example point to the 'H' in 'Hi'. E.g.

#include <stdlib.h>

unsigned long inputfromhex(const char *str, char **endPtr)
{
    return strtoul(str, endPtr, 16);
}

Hope this helps!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MafaldaCommented:
char hex_to_char(unsigned int i)
{
  char c = i % 256;
  return c;
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.