我想将二进制数转换为十进制。我的代码片段:我怎样才能获得char值转换为int?
int main(){
string count;
char number;
int counter;
int score = 0;
cout << "Wpisz liczbę w systemie binarnym" << endl;
cin >> count;
count << endl;
counter = count.length();
for (int i = 0; i < counter; i++)
{
number = count[i];
int aaa = (int) number - '0' // is that correct ? I think not.
score = aaa * pow(2,i) + score;
}
count <<wynik;
return 0;
}
但我PROGRAMM给人很奇怪的分数,例如:
输入:100(十进制8) 输出:1
输入1000(十进制16) 输出: 1
我不知道为什么。你可以帮我吗 ?我认为,那是char的问题?我真的吗?
感谢,
对不起,我Englosh
你应该为你的缩进道歉太:-) – 2013-05-01 17:16:01
这是非常有趣的:) – TN888 2013-05-01 17:18:23
字符串'中的第一个字符“100”''是'1'',所以先分配给'wynik'在该循环是'1 * pow(2,0)+ 0'。 – dyp 2013-05-01 17:18:37