为什么1 * 4 = 196?

时间:2018-12-30 19:41:21

标签: c++ binary

我试图制造一个二进制转换器,但是问题是,当我构建它时,它通常将我的测试号“ 101”解密,该数字应该为5到300-3000之间。

int main()
{

char a;
string bintered;
int bincrypted = 0, bincrypter;
cout<<"Would you like to try the binary to decimal converter? Y/N"<<endl;
cin>>a;

if (a == 'Y' || a == 'y')
{
    cout<<"Ok, enter a binary number and we will decrypt it > ";
    cin>>bintered;

    int x = bintered.length() - 1; //multiplier counting down
    int y = 0;
    int power = 0;
    while (x != -1)
    {
       power = bintered[y] * pow(2.0, x);
       bincrypted = bincrypted + power;
       y++;
       x = x - 1;
    }

    cout<<"Final answer is: "<<bincrypted<<endl;

}
return 0;
}

当我尝试执行cout<<bintered[y] * pow(2.0, x);时,它完成了三个循环,分别给了我196、96和49。 我是不正确地使用pow功能还是什么?

1 个答案:

答案 0 :(得分:2)

bintered [y]返回字符的ASCII码,因此0等于48,1等于49。