二进制转换格式字符串为int,在C [英] Convert binary format string to int, in C
本文介绍了二进制转换格式字符串为int,在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要如何转换成二进制字符串,如010011101为int,而我怎么转换成int类型,像5,在C字符串101?
How do I convert a binary string like "010011101" to an int, and how do I convert an int, like 5, to a string "101" in C?
推荐答案
如果这是他们可能希望你实现与strtol
一门功课的问题,你将有一个循环的东西像这样的:
If it is a homework problem they probably want you to implement strtol
, you would have a loop something like this:
char* start = &binaryCharArray[0];
int total = 0;
while (*start)
{
total *= 2;
if (*start++ == '1') total += 1;
}
如果你想获得幻想你可以在循环使用这些:
If you wanted to get fancy you could use these in the loop:
total <<= 1;
if (*start++ == '1') total^=1;
这篇关于二进制转换格式字符串为int,在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文