简体   繁体   English

在 C++ 中将二进制数转换为十进制数

[英]Translation from binary into decimal numbers in C++

I tried to build a function that calculates a binary number stored in a string into a decimal number stored in a long long .我试图构建一个 function ,它将存储在字符串中的二进制数计算为存储在long long中的十进制数。 I'm thinking that my code should work but it doesn't.我在想我的代码应该可以工作,但事实并非如此。

In this example for the binary number 101110111 the decimal number is 375 .在此示例中,二进制数101110111十进制数是375 But my output is completely confusing.但是我的 output 完全令人困惑。

Here is my code:这是我的代码:

#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;
    }
}

Here is the output:这是 output:

1
99
9703
894439
93131255
9132339223
894974720087
76039722530902
8583669948348758

What I'm doing wrong here?我在这里做错了什么?

You're forgetting to convert your digits into integers.您忘记将数字转换为整数。 Plus you really don't need to use C strings.另外,您真的不需要使用 C 字符串。

Here's a better version of the code这是更好的代码版本

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;
    }
}

Subtracting '0' from the string digits converts the digit into an integer.从字符串数字中减去'0'会将数字转换为 integer。

Now for extra credit write a version that doesn't use pow (hint: potency *= 2; instead of potency++; )现在为了额外的功劳,写一个不使用pow的版本(提示: potency *= 2;而不是potency++;

'1' != 1 as mentioned in the comments by @churill. '1' != 1正如@churill 的评论中提到的那样。 '1' == 49 . '1' == 49 If you are on linux type man ascii in terminal to get the ascii table.如果您在 linux 上,请在终端中键入man ascii以获取 ascii 表。

Try this, it is the same code.试试这个,它是相同的代码。 I just used the stringNumber directly instead of using const char* to it.我只是直接使用了stringNumber而不是对它使用const char* And I subtracted '0' from the current index.我从当前索引中减去了'0' '0' == 48 , so if you subtract it, you get the actual 1 or 0 integer value: '0' == 48 ,所以如果你减去它,你会得到实际的10 integer 值:

    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;
    }

Moreover, use the methods provided by std::string like .size() instead of doing strlen() on every iteration.此外,使用std::string提供的方法,例如.size() ,而不是在每次迭代时执行strlen() Much faster.快多了。


In a production environment, I would highly recommend using std::bitset instead of rolling your own solution:在生产环境中,我强烈建议使用std::bitset而不是滚动您自己的解决方案:

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

c++ way 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM