c ++两位十六进制到十进制转换器

时间:2015-11-08 18:54:46

标签: c++ hex data-conversion

我正在尝试编写一个允许用户输入两位十六进制数并将其转换为整数的程序。唯一的问题是我无法让用户在程序中输入char值。这是我现在的代码。

#include <iostream>

using namespace std;

int main()
{
int val1, val2;
int decval;

cout << "Enter the first hex value: ";
cin >> val1;
cout << "Enter the second hex value: ";
cin >> val2;

if (val1 >=65 && val1 <=70)
{
val1 = (val1-55)*16;
    if (val2 >= 65 && val2 <= 70)
    {
        val2 -= 55;
        decval = val1+val2;
        cout << decval;
    }
    else if (val2 >= 0 && val2 <=9)
    {
        val2 = val2;
        decval = val1 + val2;
        cout << decval;
    }
    else
       cout << "Error";
}
else if (val1 >= 0 && val1 <=9)
{
    val1 = val1*16;
    if (val2 >= 65 && val2 <= 70)
    {
        val2 -= 55;
        decval = val1+val2;
        cout << decval;
    }
    else if (val2 >= 0 && val2 <=9)
    {
        decval = val1 + val2;
        cout << decval;
    }
}
else
{
    cout << "Error";
}

0 个答案:

没有答案