954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Conversion from char* (or unsigned char*) to unsigned int

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

lil_panda
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

strtol()

If you want to use pure c++ then use stringstream c++ class.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

lil_panda
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

still use strtol()

int main()
{
    char hex[]= "ABCD";
    char* end = 0;
    unsigned int num = strtol(hex,&end,16);
    cout << num << "\n";
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

lil_panda
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 
>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.

lil_panda
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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';
}
Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You