How do I convert a char* or an unsigned char* to an unsigned int?

Let me put this a different way.
I want to get the hex value of a number and insert it into an unsigned int, including the preceding 0x.
How would I do that?

still use strtol()

int main()
{
    char hex[]= "ABCD";
    char* end = 0;
    unsigned int num = strtol(hex,&end,16);
    cout << num << "\n";
}

>still use strtol()
Better to use stringstream:

#include <ios>
#include <iostream>
#include <sstream>

int main()
{
  using namespace std;

  istringstream is("ABCD");
  unsigned int num;

  if (is >> hex >> num)
    cout << num << '\n';
}
// Confusing name choice
char hex[]= "ABCD";

hex is also the name of a standard manipulator defined in <ios>. In the worst case you have a name collision and at best you have confusing results if you try to print num as hex:

// Prints "ABCD43981", not "abcd"
cout << hex << num << '\n';

Adding an explicit qualifier fixes the problem, but that's not an obvious fix without a bit of head scratching:

// Prints "abcd" as expected
cout << std::hex << num << '\n';

>How do I convert a char* or an unsigned char* to an unsigned int?
All of the above assumes char*. If you have an unsigned char* that's different because there's a distinct difference in type between unsigned char* and C strings. To use C string solutions with unsigned char* you have to somehow convert it to char* either by assignment to a temporary or by casting if you know it's a safe conversion.

Comments
Agree -- you have a better solution for c++

I originally tried the stringstream thing similar to what you posted but it wouldn't work for me -- just proeuced some gigantic number. After a little more testing I find that the problem was becuse of my use of hex[].

The code

#include <ios>
#include <iostream>
#include <sstream>
 
int main()
{
  using namespace std;
 
  istringstream is("ABCD");
  unsigned int num;
 
  if (is >> hex >> num)
    cout << num << '\n';
}

suggested by Radical Edward prints 31 less than the actual hex value. Is there a way to fix this without adding 31 to the unsigned int num ?????

>prints 31 less than the actual hex value
What exactly is it printing that's wrong? Ed gets 43981, which is the correct value.

>prints 31 less than the actual hex value
What exactly is it printing that's wrong? Ed gets 43981, which is the correct value.

When is = "A" , 10 is printed out but the actual hex value of "A" is 0x41.
When is = "B" , 11 is printed out but the actual hex value of "B" is 0x42.
and so on.

The whole reason a stringstream is used is to turn the hexadecimal number represented by the string "ABCD" into the integer number 43981. In other words, the character that each value (0x41, 0x42, etc...) represents is used instead of the actual value. If you want to get the actual values, don't do the conversion and just print the character cast to an int:

#include <iostream>
 
int main()
{
  const char *p = "ABCD";
 
  for (int i = 0; p[i] != '\0'; ++i)
    std::cout << std::hex << int(p[i]) << '\n';
}
This article has been dead for over six months. Start a new discussion instead.