<分区>
我在网上搜索过,似乎并不能解决我的问题。基本上我有一个 std::string
,它包含一个十六进制内存地址(例如 0x10FD7F04)。显然,这个数字是从文本文件中读取并保存为 std::string
。
我需要将此字符串转换为 int 值,但保留十六进制表示法 0x。有什么办法吗?
<分区>
我在网上搜索过,似乎并不能解决我的问题。基本上我有一个 std::string
,它包含一个十六进制内存地址(例如 0x10FD7F04)。显然,这个数字是从文本文件中读取并保存为 std::string
。
我需要将此字符串转换为 int 值,但保留十六进制表示法 0x。有什么办法吗?
最佳答案
您可以使用 C++11 std::stoi
功能:
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string your_string_rep{"0x10FD7F04"};
int int_rep = stoi(your_string_rep, 0, 16);
std::cout << int_rep << '\n';
std::cout << std::hex << std::showbase << int_rep << '\n';
}
输出:
285048580
0x10fd7f04
关于c++ - 将 std::string 数字转换为十六进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25685303/