Huawei's R & D engineers programming problem: hexadecimal decimal conversion

One, Title Description

Write a program that accepts a hexadecimal number, the output value of the decimal representation. (Plurality of sets of simultaneous input)

Second, problem-solving ideas

  • Read one by one character string, is converted
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void sln(string str)
{
    auto len = str.size();
    long long sln = 0;
    for (size_t i = 2; i < len; i++)
    {
        unsigned short tmp;
        if (str[i] >= 'a' && str[i] <= 'f')
        {
            tmp = str[i] - 'a' + 10;
        }
        else if (str[i] >= 'A' && str[i] <= 'F')
            tmp = str[i] - 'A' + 10;
        else if (str[i] >= '0' && str[i] <= '9')
            tmp = str[i] - '0';
        else
        {
            cout << "Error!" << endl;
            return;
        }
        sln = sln * 16 + tmp;
    }
    cout << sln << endl;
}
int main()
{
    string str;
    while (cin >> str)
    {
        sln(str);
    }
    return 0;
}
  • C ++ using binary input and output method
#include <iostream>
using namespace std;

int main()
{
    int a;
    while(cin>>hex>>a){
    cout<<a<<endl;
    }
}
Published 30 original articles · won praise 3 · Views 811

Guess you like

Origin blog.csdn.net/weixin_44587168/article/details/105370680