0

How can I convert it? And I want the converted result to 1.

#include <iostream> #include <string> int main(int argc, const char * argv[]) { std::string s = "0x00"; // insert code here... unsigned char* str_hex = (unsigned char*)s.c_str(); unsigned char target = 0x00; std::cout << "result: " << (*str_hex == target) << "\n"; return 0; } 
2

1 Answer 1

1

Like this: // #include <string>

std::string s = "0x4A"; unsigned char ch = std::stoul(s, nullptr, 16); // 'J' (Since C++11) 

Or like this: // #include <cstdio>

std::string s = "0x4B"; unsigned char ch = '\0'; sscanf(s.c_str(), "%x", &ch); // 'K' (C library function) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.