【问题标题】:Translation from binary into decimal numbers in C++在 C++ 中将二进制数转换为十进制数
【发布时间】:2020-10-27 09:01:57
【问题描述】:

我尝试构建一个函数,将存储在字符串中的二进制数计算为存储在long long 中的十进制数。我在想我的代码应该可以工作,但它没有。

在这个二进制数 101110111 的示例中,十进制数是 375。但我的输出完全令人困惑。

这是我的代码:

#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>

int main() {
    std::string stringNumber = "101110111";

    const char *array = stringNumber.c_str();
    int subtrahend = 1;
    int potency = 0;
    long long result = 0;

    for(int i = 0; i < strlen(array); i++) {
        result += pow(array[strlen(array) - subtrahend] * 2, potency);
        subtrahend++;
        potency++;
        std::cout << result << std::endl;
    }
}

这是输出:

1
99
9703
894439
93131255
9132339223
894974720087
76039722530902
8583669948348758

我在这里做错了什么?

【问题讨论】:

  • 可能不相关:您可以像使用const char* 一样使用std::string,因为它重载了下标运算符[]。它甚至更好,因为它有一个length()-function,您可以使用它来代替strlen。您可以完全删除 array。
  • 还有一点提示:'1' != 1 但'1' == 49

标签: c++ binary decimal calculation


【解决方案1】:

@churill 在 cmets 中提到的'1' != 1。 '1' == 49。如果您使用的是 linux,请在终端中输入 man ascii 以获取 ascii 表。

试试这个,它是相同的代码。我只是直接使用了stringNumber,而不是使用const char*。我从当前索引中减去了'0'。 '0' == 48,所以如果你减去它,你会得到实际的1 或0 整数值:

    auto sz = stringNumber.size();

    for(int i = 0; i < sz; i++) {
        result += pow((stringNumber[sz - subtrahend] - '0') * 2, potency);
        subtrahend++;
        potency++;
        std::cout << result << std::endl;
    }

此外,请使用std::string 提供的方法,例如.size(),而不是在每次迭代时都使用strlen()。更快。


在生产环境中,我强烈建议使用std::bitset 而不是滚动您自己的解决方案:

    std::string stringNumber = "1111";
    std::bitset<64> bits(stringNumber);
    bits.to_ulong();

【讨论】:

    【解决方案2】:

    您忘记将数字转换为整数。另外,您真的不需要使用 C 字符串。

    这是一个更好的代码版本

    int main() {
        std::string stringNumber = "101110111";
    
        int subtrahend = 1;
        int potency = 0;
        long long result = 0;
    
        for(int i = 0; i < stringNumber.size(); i++) {
            result += pow(2*(stringNumber[stringNumber.size() - subtrahend] - '0'), potency);
            subtrahend++;
            potency++;
            std::cout << result << std::endl;
        }
    }
    

    从字符串数字中减去'0' 会将数字转换为整数。

    现在写一个不使用 pow 的版本(提示:potency *= 2; 而不是 potency++;)

    【讨论】:

      【解决方案3】:

      c++方式

      #include <string>
      #include <math.h>
      #include <iostream>
      
      
      using namespace std;
      int main() {
          std::string stringNumber = "101110111";   
          long long result = 0;
      
          uint string_length = stringNumber.length();
      
          for(int i = 0; i <string_length; i++) {
            if(stringNumber[i]=='1')
            {
              long pose_value = pow(2, string_length-1-i);        
              result += pose_value;
            }       
              
          }
          std::cout << result << std::endl;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-21
        • 2020-09-10
        • 2011-01-08
        • 2010-12-08
        • 1970-01-01
        • 2012-12-26
        • 2017-02-18
        相关资源
        最近更新 更多