C ++:将十六进制转换为十进制
问题描述:
我正在寻找一种将hex
(十六进制)轻松转换为dec
(十进制)的方法.我找到了一种简单的方法:
I'm looking for a way to convert hex
(hexadecimal) to dec
(decimal) easily. I found an easy way to do this like :
int k = 0x265;
cout << k << endl;
但是,我不能输入265
.无论如何,它可以像这样工作:
But with that I can't input 265
. Is there anyway for it to work like that:
输入:265
输出:613
反正有这样做吗?
注意:我已经尝试过:
int k = 0x, b;
cin >> b;
cout << k + b << endl;
它不起作用.
答
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
int x, y;
std::stringstream stream;
std::cin >> x;
stream << x;
stream >> std::hex >> y;
std::cout << y;
return 0;
}