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;
}
Amit G.
  • 2,546
  • 2
  • 22
  • 30
Jet C.
  • 126
  • 8
  • 1
    How would you do it on paper by hand? Write down the steps you take there and use that as template for the program. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 09 '20 at 06:30
  • 4
    https://stackoverflow.com/questions/10156409/convert-hex-string-char-to-int – Retired Ninja Nov 09 '20 at 06:31

1 Answers1

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)
Amit G.
  • 2,546
  • 2
  • 22
  • 30