【问题标题】:Converting HEX to DEC [duplicate]将 HEX 转换为 DEC [重复]
【发布时间】:2020-09-12 11:45:19
【问题描述】:

我正在尝试使用 C++ 程序将十六进制值转换为十进制值。就是想不出一个有效的代码。

这是我想出的最好的东西:

int main () {
    string link;
    string hex_code;
    int dec_code;
    int i;
    int n = 6;
    int num;
    int hex;

    cout << "Insert 1 of the HEX characters at a time:";

    for (i = 0; i < n; i++) {
        cin >> hex_code;
    }
    for (i = 0; i < n; i++) {
        if (hex_code == "A") {
            hex_code = 10;
        }
        else if (hex_code == "B") {
            hex_code = 11;
        }
        else if (hex_code == "C") {
            hex_code = 12;
        }
        else if (hex_code == "D") {
            hex_code = 13;
        }
        else if (hex_code == "E") {
            hex_code = 14;
        }
        else if (hex_code == "F") {
            hex_code = 15;
        }
        else {
            hex_code = hex_code;
        }
        num = hex * pow (16, i);
    }
    for (i = 0; i < n; i++) {
        dec_code = dec_code + num;
    }
    cout << dec_code;
return 0;
}

欢迎任何帮助/反馈/意见。

编辑:感谢您的所有帮助。在这里找到了我尝试创建但失败的代码:https://stackoverflow.com/a/27334556/13615474

【问题讨论】:

  • dec_code = dec_code + num 应该在上面的 for 循环中
  • hex_codestd::string 时,hex_code = 10 毫无意义。应该是hex = 10;,六个地方有同样的问题。

标签: c++ hex decimal


【解决方案1】:

C++的iomanip库中有一个十六进制操作符

   cout << "Insert 1 of the HEX characters at a time:";

    for (i = 0; i < n; i++) {
        int hexcode;
        std::cin >> std::hex >> hexcode;
        std::cout << hexcode << std::endl;
    }

这将打印给定十六进制代码的十进制等效项

【讨论】:

    【解决方案2】:

    可以通过将输入数字读取为字符数组并对每个字符执行转换算术来执行十六进制到十进制的转换。

    这是一个将十六进制转换为十进制的工作示例:

    // File name: HexToDec.cpp    
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int hexToDec(char hexNumber[]) {
        int decimalNumber = 0;
        int len = strlen(hexNumber);
        for (int base = 1, i=(len-1); i>=0; i--, base *= 16) {
           // Get the hex digit in upper case 
           char digit = toupper(hexNumber[i]);
           if ( digit >= '0' && digit <='9' ) {
              decimalNumber += (digit - 48)*base;
           }
           else if ( digit >='A' && digit <='F' ) {
              decimalNumber += (digit - 55)*base;
           }
        }
        return decimalNumber;
    }
    
    int main() {
    
       char hexNumber[80];
       // Read the hexadecimal number as a character array
       cout << "Enter hexadecimal number: ";
       cin >> hexNumber;
       cout << hexNumber << " in decimal format = " << hexToDec(hexNumber) << "\n";
       return 0;
    }
    

    输出:

    Enter hexadecimal number: DEC
    DEC in decimal format = 3564
    

    更多信息:

    https://www.geeksforgeeks.org/program-for-hexadecimal-to-decimal/

    【讨论】:

      【解决方案3】:

      这是一个使用查找表的简单代码片段:

      char c;
      static const char hex_to_decimal[] = "0123456789ABCDEF";
      std::cin >> c;
      int decimal = 0;
      for (decimal = 0; decimal < sizeof(hex_to_decimal) - 1; ++decimal)
      {
        if (hex_to_decimal[i] == c)
        {
            break;
        }
      }
      

      另一种转换方式:

      std::cin >> c;
      int decimal;
      if (is_digit(c))
      {
          decimal = c - '0';
      }
      else
      {
          decimal = 10 + c - 'A';
      }
      

      上面的代码片段假设编码有'A'...'F'连续。 第一个例子更便携。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-11
        • 2020-02-10
        • 2011-07-03
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多