c++输入十六进制转十进制
时间: 2023-11-30 20:01:58 浏览: 79
c++ 十六进制转换十进制
4星 · 用户满意度95%
以下是C++中输入十六进制转十进制的示例代码:
```c++
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int hexToDecimal(string hex) {
int decimal = 0;
int base = 1;
int len = hex.length();
for (int i = len - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
decimal += (hex[i] - 48) * base;
base *= 16;
}
else if (hex[i] >= 'A' && hex[i] <= 'F') {
decimal += (hex[i] - 55) * base;
base *= 16;
}
}
return decimal;
}
int main() {
string hex;
cout << "请输入一个十六进制数:";
cin >> hex;
int decimal = hexToDecimal(hex);
cout << "转换成十进制为:" << decimal << endl;
return 0;
}
```
阅读全文