943,769 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7821
  • C++ RSS
Aug 11th, 2008
0

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

Expand Post »
How do I convert a char* or an unsigned char* to an unsigned int?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
lil_panda is offline Offline
33 posts
since Jul 2008
Aug 11th, 2008
0

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

strtol()

If you want to use pure c++ then use stringstream c++ class.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 11th, 2008
0

Re: Conversion from char* (or unsigned char*) to 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?
Last edited by lil_panda; Aug 11th, 2008 at 12:39 am. Reason: Forgot something
Reputation Points: 10
Solved Threads: 0
Light Poster
lil_panda is offline Offline
33 posts
since Jul 2008
Aug 11th, 2008
0

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

still use strtol()
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. char hex[]= "ABCD";
  4. char* end = 0;
  5. unsigned int num = strtol(hex,&end,16);
  6. cout << num << "\n";
  7. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 11th, 2008
1

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

>still use strtol()
Better to use stringstream:
C++ Syntax (Toggle Plain Text)
  1. #include <ios>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. istringstream is("ABCD");
  10. unsigned int num;
  11.  
  12. if (is >> hex >> num)
  13. cout << num << '\n';
  14. }
// 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:
C++ Syntax (Toggle Plain Text)
  1. // Prints "ABCD43981", not "abcd"
  2. cout << hex << num << '\n';
Adding an explicit qualifier fixes the problem, but that's not an obvious fix without a bit of head scratching:
C++ Syntax (Toggle Plain Text)
  1. // Prints "abcd" as expected
  2. 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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 11th, 2008
0

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

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[].
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 12th, 2008
0

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

The code
C++ Syntax (Toggle Plain Text)
  1. #include <ios>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. istringstream is("ABCD");
  10. unsigned int num;
  11.  
  12. if (is >> hex >> num)
  13. cout << num << '\n';
  14. }

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 ?????
Last edited by lil_panda; Aug 12th, 2008 at 1:29 pm. Reason: code tags
Reputation Points: 10
Solved Threads: 0
Light Poster
lil_panda is offline Offline
33 posts
since Jul 2008
Aug 12th, 2008
0

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

>prints 31 less than the actual hex value
What exactly is it printing that's wrong? Ed gets 43981, which is the correct value.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 12th, 2008
0

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

>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.
Last edited by lil_panda; Aug 12th, 2008 at 2:45 pm. Reason: hex values
Reputation Points: 10
Solved Threads: 0
Light Poster
lil_panda is offline Offline
33 posts
since Jul 2008
Aug 12th, 2008
0

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

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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. const char *p = "ABCD";
  6.  
  7. for (int i = 0; p[i] != '\0'; ++i)
  8. std::cout << std::hex << int(p[i]) << '\n';
  9. }
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Returning pointers
Next Thread in C++ Forum Timeline: Need help with ofstream !!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC