在 C++ 中将十六进制字符串转换为无符号字符
Convert hex string to unsigned char in C++
如何转换?我希望转换后的结果为 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;
}
像这样:// #include <string>
std::string s = "0x4A";
unsigned char ch = std::stoul(s, nullptr, 16); // 'J' (Since C++11)
或者像这样:// #include <cstdio>
std::string s = "0x4B";
unsigned char ch = '[=11=]';
sscanf(s.c_str(), "%x", &ch); // 'K' (C library function)
如何转换?我希望转换后的结果为 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;
}
像这样:// #include <string>
std::string s = "0x4A";
unsigned char ch = std::stoul(s, nullptr, 16); // 'J' (Since C++11)
或者像这样:// #include <cstdio>
std::string s = "0x4B";
unsigned char ch = '[=11=]';
sscanf(s.c_str(), "%x", &ch); // 'K' (C library function)