如何解决在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 = '\0';
sscanf(s.c_str(),"%x",&ch); // 'K' (C library function)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。