华为机试 — 进制转换

题目描述

写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )

输入描述:

输入一个十六进制的数值字符串。

输出描述:

输出该数值的十进制字符串。

示例1

输入

0xA

输出

10
#include<iostream>
#include<string>
using namespace std;
int main(){
    string str1;
    while(cin>>str1){
        str1 = str1.substr(2,str1.length()-2);
        int str1_len = str1.length();
        int res = 0;
        for (int i = 0; i < str1_len; i++){
            if (str1[i] >= '0' && str1[i] <= '9'){
                res = res * 16 + str1[i] - '0';
            }
            else if (str1[i] >= 'A' && str1[i] <= 'F'){
                res = res * 16 + str1[i] - 'A' + 10;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ChesterWNimitz/article/details/81461813