十六进制转换为十进制

时间:2025-03-24 20:41:25

问题描述:
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串(注意可能存在的一个测试用例里的多组数据)。

输入:
0xA

输出:
10

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<char> conclude;


int CharToInt(char x){
    if (x >= '0' && x <= '9'){
        return x - '0';
    }else{
        return x - 'A' + 10;
    }
}
void Convert(string str, int x){
    int number = 0;
    for(int i = 0; i < str.size(); ++i){
        number *= x;
        number += CharToInt(str[i]);
    }
    printf("%d\n", number);
}

int main(){
    string str;
    while (cin >> str){
        str = str.substr(2);
        Convert(str, 16);
    }
    return 0;
}