我正在寻找一种将 hex
(十六进制) 转换为 dec
(十进制) 的简单方法。我发现了一个简单的方法,就像这样:
int k = 0x265;
cout << k << endl;
但是我不能输入 265
,有没有办法让它像这样工作:
输入: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;
}
cin >> hex >> y
? - Oliver Charlesworth使用std::hex
操纵器:
#include <iostream>
#include <iomanip>
int main()
{
int x;
std::cin >> std::hex >> x;
std::cout << x << std::endl;
return 0;
}
std::cin >> std::dec;
将cin
恢复为使用十进制。 - hmjdp
。这篇文章回答了你所发布的问题 :( - Mooing Duck#include <stdlib.h>
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%X", n);
exit(0);
}
以下是一种使用字符串并将其转换为十进制的 ASCII 表格的解决方案:
#include <iostream>
#include <string>
#include "math.h"
using namespace std;
unsigned long hex2dec(string hex)
{
unsigned long result = 0;
for (int i=0; i<hex.length(); i++) {
if (hex[i]>=48 && hex[i]<=57)
{
result += (hex[i]-48)*pow(16,hex.length()-i-1);
} else if (hex[i]>=65 && hex[i]<=70) {
result += (hex[i]-55)*pow(16,hex.length( )-i-1);
} else if (hex[i]>=97 && hex[i]<=102) {
result += (hex[i]-87)*pow(16,hex.length()-i-1);
}
}
return result;
}
int main(int argc, const char * argv[]) {
string hex_str;
cin >> hex_str;
cout << hex2dec(hex_str) << endl;
return 0;
}
template <typename T>
bool fromHex(const std::string& hexValue, T& result)
{
std::stringstream ss;
ss << std::hex << hexValue;
ss >> result;
return !ss.fail();
}
std::cout << "Enter decimal number: " ;
std::cin >> input ;
std::cout << "0x" << std::hex << input << '\n' ;
#include <iostream>
using namespace std;
template <class T> // function template
T square(T); /* returns a value of type T and accepts type T (int or float or whatever) */
void main()
{
int x, y;
float w, z;
cout << "Enter a integer: ";
cin >> x;
y = square(x);
cout << "The square of that number is: " << y << endl;
cout << "Enter a float: ";
cin >> w;
z = square(w);
cout << "The square of that number is: " << z << endl;
}
template <class T> // function template
T square(T u) //accepts a parameter u of type T (int or float)
{
return u * u;
}
Here is the output:
Enter a integer: 5
The square of that number is: 25
Enter a float: 5.3
The square of that number is: 28.09
只使用:
cout << dec << 0x;
int base = 16;
std::string numberString = "0xa";
char *end;
long long int number;
number = strtoll(numberString.c_str(), &end, base);
我认为这更清晰,而且也能够处理你的异常。
#include <iostream>
using namespace std;
using ll = long long;
int main ()
{
ll int x;
cin >> hex >> x;
cout << x;
}
std::stoi, stol, stoul, stoull 可以将数字转换为不同的进制
long long hex2dec(std::string hex)
{
std::string::size_type sz = 0;
try
{
hex = "0x"s + hex;
return std::stoll(hex, &sz, 16);
}
catch (...)
{
return 0;
}
}
如果你需要返回字符串,可以使用类似的方法
std::string hex2decstr(std::string hex)
{
std::string::size_type sz = 0;
try
{
hex = "0x"s + hex;
return std::to_string(std::stoull(hex, &sz, 16));
}
catch (...)
{
return "";
}
}
使用方法:
std::string converted = hex2decstr("16B564");
std::from_chars
和std::to_chars
? - MatG
0x265
在十进制中是613。你期望的是什么? - Oliver Charlesworth