基础练习 十六进制转十进制

题目链接

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
long long Dec;
int main()
{
    string Hex;
    int HexLength;

    while(cin >> Hex)
    {
        HexLength = Hex.length();
        Dec = 0;
        for(int i = HexLength - 1; i >= 0; --i){
            if(Hex[i] >= '0' && Hex[i] <= '9')
                Dec += pow(16, HexLength - 1 - i) * (Hex[i] - '0');
            else if(Hex[i] >= 'A' && Hex[i] <= 'F')
                Dec += pow(16, HexLength - 1 - i) * (Hex[i] - 'A' + 10);
        }
        cout << Dec << endl;
    }

}
原文地址:https://www.cnblogs.com/XuYiting/p/9704376.html