编写一个将二进制字符串转换为数字值的函数。
#include <stdio.h>
int convertBinaryStringToInt(const char * string);
int main(void)
{
int result;
char * binstring = "01001001";
printf("%s in base-10 is %d./n", binstring, convertBinaryStringToInt(binstring));
return 0;
}
int convertBinaryStringToInt(const char * string)
{
// convert a binary string to a numeric value
int retval = 0;
while (*string != '/0')
{
retval <<= 1;
if (*string == '1')
retval |= 1;
string++;
}
return retval;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/266546.html