cpp 数值转16进制的库
C++标准库中可以使用以下函数将数值转换为16进制字符串:
-
std::hex:将整数以16进制格式输出。 -
std::stringstream:可以将各种类型的数据转换成字符串。
示例代码:
#include <iostream>
#include <sstream>
int main()
{
int value = 123;
std::stringstream ss;
ss << std::hex << value;
std::string result = ss.str();
std::cout << "Value in hexadecimal: " << result << std::endl;
return 0;
}
输出:
Value in hexadecimal: 7b
另外,C++11标准引入了 std::to_string 函数,可以将整数等基本类型数据转换为字符串。使用该函数和 std::hex 一起可以方便地将数值转换为16进制字符串,示例代码如下:
#include <iostream>
int main()
{
int value = 123;
std::string result = std::to_string(value);
std::stringstream ss;
ss << std::hex << value;
result = ss.str();
std::cout << "Value in hexadecimal: " << result << std::endl;
return 0;
}
输出:
Value in hexadecimal: 7b
原文地址: https://www.cveoy.top/t/topic/b7zj 著作权归作者所有。请勿转载和采集!