c++ convert string to hex
96,657
Solution 1
Use std:stoi
as (in C++11 only):
std::string s = "5f0066";
int num = std::stoi(s, 0, 16);
Solution 2
Use stringstream and std::hex
std::stringstream str;
std::string s1 = "5f0066";
str << s1;
int value;
str >> std::hex >> value;
Solution 3
strtol(str, NULL, 16)
or strtoul(str, NULL, 16)
should do what you need.
Author by
Homer
Updated on August 01, 2022Comments
-
Homer over 1 year
Possible Duplicate:
C++ convert hex string to signed integerI allready searched on google but didn't find any help. So heres my problem: I have strings that allready contains hex code e.g.: string s1 = "5f0066" and i want to convert this string to hex. I need to compare the string with hex code i've read in a binary file.
(Just need the basic idea how to convert the string) 5f Best regards and thanks.
Edit: Got the binary file info in the format of unsigned char. SO its 0x5f... WOuld like to have teh string in the same format