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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 33
Reputation: lil_panda is an unknown quantity at this point 
Solved Threads: 0
lil_panda lil_panda is offline Offline
Light Poster

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

 
0
  #1
Aug 11th, 2008
How do I convert a char* or an unsigned char* to an unsigned int?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Aug 11th, 2008
strtol()

If you want to use pure c++ then use stringstream c++ class.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: lil_panda is an unknown quantity at this point 
Solved Threads: 0
lil_panda lil_panda is offline Offline
Light Poster

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

 
0
  #3
Aug 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #4
Aug 11th, 2008
still use strtol()
  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. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

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

 
1
  #5
Aug 11th, 2008
>still use strtol()
Better to use stringstream:
  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:
  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:
  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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #6
Aug 11th, 2008
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[].
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: lil_panda is an unknown quantity at this point 
Solved Threads: 0
lil_panda lil_panda is offline Offline
Light Poster

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

 
0
  #7
Aug 12th, 2008
The code
  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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

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

 
0
  #8
Aug 12th, 2008
>prints 31 less than the actual hex value
What exactly is it printing that's wrong? Ed gets 43981, which is the correct value.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: lil_panda is an unknown quantity at this point 
Solved Threads: 0
lil_panda lil_panda is offline Offline
Light Poster

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

 
0
  #9
Aug 12th, 2008
Originally Posted by Radical Edward View Post
>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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

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

 
0
  #10
Aug 12th, 2008
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:
  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. }
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3356 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC