| | |
Conversion from char* (or unsigned char*) to unsigned int
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
still use strtol()
C++ Syntax (Toggle Plain Text)
int main() { char hex[]= "ABCD"; char* end = 0; unsigned int num = strtol(hex,&end,16); cout << num << "\n"; }
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.
>still use strtol()
Better to use stringstream:
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:
Adding an explicit qualifier fixes the problem, but that's not an obvious fix without a bit of head scratching:
>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.
Better to use stringstream:
C++ Syntax (Toggle Plain Text)
#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"; C++ Syntax (Toggle Plain Text)
// Prints "ABCD43981", not "abcd" cout << hex << num << '\n';
C++ Syntax (Toggle Plain Text)
// Prints "abcd" as expected cout << std::hex << num << '\n';
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.
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.
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
Solved Threads: 0
The code
suggested by Radical Edward prints 31 less than the actual hex value. Is there a way to fix this without adding 31 to the
C++ Syntax (Toggle Plain Text)
#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 ????? Last edited by lil_panda; Aug 12th, 2008 at 1:29 pm. Reason: code tags
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
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.
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
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)
#include <iostream> int main() { const char *p = "ABCD"; for (int i = 0; p[i] != '\0'; ++i) std::cout << std::hex << int(p[i]) << '\n'; }
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- Command Line Arguement, can't get it to read in an int (C)
- conversion trouble (UCHAR to *Char? wth) (C++)
- number system conversion (C++)
- How to flip bits (C)
- Data loss in Type Conversion. (C++)
- Getting all data from an input and output file (C++)
- i need code for binary to decimal (it should be written in 'C') (C)
- text to numbers, numbers to text (C)
- C craps game (C)
Other Threads in the C++ Forum
- Previous Thread: Returning pointers
- Next Thread: Need help with ofstream !!!
Views: 3356 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






