以C++格式将十六进制字符串转换为十进制数
Convert Hexadecimal string to decimal number in C++
我想在C++中将十六进制字符串转换为十进制数(整数(,并尝试了以下方法:
std::wstringstream SS;
SS << std::dec << stol(L"0xBAD") << endl;
但它返回0
而不是2989
.
std::wstringstream SS;
SS << std::dec << reinterpret_cast<LONG>(L"0xBAD") << endl;
但它返回-425771592
而不是2989
.
但是,当我像下面这样使用它时,它工作正常并按预期提供2989
。
std::wstringstream SS;
SS << std::dec << 0xBAD << endl;
但是我想输入一个字符串并获取2989
作为输出,而不是像0xBAD
这样的整数输入。例如,我想输入"0xBAD"
并将其转换为整数,然后转换为十进制数。
提前谢谢。
// stol example
#include <iostream> // std::cout
#include <string> // std::string, std::stol
int main ()
{
std::string str_dec = "1987520";
std::string str_hex = "2f04e009";
std::string str_bin = "-11101001100100111010";
std::string str_auto = "0x7fffff";
std::string::size_type sz; // alias of size_t
long li_dec = std::stol (str_dec,&sz);
long li_hex = std::stol (str_hex,nullptr,16);
long li_bin = std::stol (str_bin,nullptr,2);
long li_auto = std::stol (str_auto,nullptr,0);
std::cout << str_dec << ": " << li_dec << 'n';
std::cout << str_hex << ": " << li_hex << 'n';
std::cout << str_bin << ": " << li_bin << 'n';
std::cout << str_auto << ": " << li_auto << 'n';
return 0;
}
相关文章:
- visual在c++中将十进制数转换为二进制数
- C++从二进制转换为十进制数
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 将分数转换为十进制数并对其进行排序
- 使用标准库计算字符串中的十进制数
- (第 1 位 + 第 2 位)以十进制数表示
- Pybind11:在C++中使用Pybind11转换numpy数组的问题
- 如何通过输入十进制数打印UTF-8符号?
- 如何使用链表将负十进制数转换为二进制?
- 二进制反转为十进制数
- 以C++格式将十六进制字符串转换为十进制数
- 将字节数组转换为 C++ 中的十进制数
- 您能否给我一个 16 位(或更多)十进制数,该十进制数仅在第 15 位正确转换为双精度浮点圆
- 使用堆栈将十进制数转换为二进制数
- 将浮点数转换为字符串,并指定精度和十进制数字数?
- 将十进制数转换为包装 BCD,反之亦然
- 转换字符串十进制数时的精度
- C++Cout将字符串转换为保留所有小数的十进制数
- 将十进制数转换为二进制时出现意外结果
- 将二进制向量转换为十进制数组