[Pointers and C-String]D. Liang 7.14 Binary to decimal
Description:
Write a function that parses a binary number as a string into a decimal integer.
The function header is as follows:
int parseBinary(const char * const binaryString)
For example, binaryString "10001"
is 17 (12^4 + 02^3 + 02^2 + 02 + 1 = 17). So, parseBinary("10001")
returns 17.
Hint:
Don’t submit the main() function.
Programme:
//Date:2020/5/18
//Author:Kamenrider Justice
int parseBinary(const char * const binaryString)//和十六进制转十进制基本一致
{
int length,i,sum=0;
length=strlen(binaryString);//获取位数
for(i=0;i<length;i++)
{
sum=sum*2+binaryString[i]-'0';
}
return sum;
}
Java面试Offer直通车