Tag Archives: Niuke net study notes

Conversion from hexadecimal to decimal

Problem Description:
write a program that accepts a hexadecimal value string and outputs the decimal string of the value (note that there may be multiple sets of data in a test case).

Input:
0xa

Output:
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;
}