![](/img/trans.png)
[英]How to Convert hexadecimal integer into a string in c++ (for html colors)?
[英]how to convert a hexadecimal string to a corresponding integer in c++?
我有一个存储在文件中的unicode映射。
像下面这一行用制表符分隔。
a 0B85 0 0B85
第二列是unicode字符。 我想将其转换为0x0B85,它将存储在int变量中。
怎么做?
您可以使用strtol
,它可以将数字解析为long,然后您可以将其分配给int
。 strtol
可以使用2到36的任何基数来解析数字(即任何可以用字母数字字符表示的基数)。
例如:
#include <cstdlib>
using namespace std;
char *token;
...
// assign data from your file to token
...
char *err; // points to location of error, or final '\0' if no error.
int x = strtol(token, &err, 16); // convert hex string to int
您已经要求使用C ++,所以这里是使用流的规范C ++解决方案:
#include <iostream>
int main()
{
int p;
std::cin >> std::hex >> p;
std::cout << "Got " << p << std::endl;
return 0;
}
如果在您的情况下需要,可以用std::cin
替换字符串流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.