Convert Hex to decimal in C/C++?

Posted on 2003-02-27
Medium Priority
Last Modified: 2012-08-13
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.

Question by:abayyari
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8040009
  #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
LVL 14

Accepted Solution

Tommy Braas earned 200 total points
ID: 8040149
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!

Expert Comment

ID: 8040719
char hex_to_char(unsigned int i)
  char c = i % 256;
  return c;

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question