C ++ convert string to hex

Possible duplicate:
C ++ convert hexadecimal string to signed integer

I already searched on google but did not find any help. So here is my problem: I have strings that allready contain hexadecimal code, for example: string s1 = "5f0066" and I want to convert this string to hex. I need to compare a string with the hex code I read in a binary.

(You just need the basic idea of ​​how to convert the string) 5f Best regards and thanks.

Edit: Got binary information in unsigned char format. SO its 0x5f ... It would be desirable if the string were in the same format

+4
source share
3 answers

Use stringstream and std :: hex

std::stringstream str; std::string s1 = "5f0066"; str << s1; int value; str >> std::hex >> value; 
+12
source

Use std:stoi as (only in C ++ 11):

 std::string s = "5f0066"; int num = std::stoi(s, 0, 16); 

Online demo

+8
source

strtol(str, NULL, 16) or strtoul(str, NULL, 16) should do what you need.

strtol strtoul

+6
source

All Articles