C语言 在 C 中将二进制格式字符串转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2343099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert binary format string to int, in C
提问by Yongwei Xing
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?
如何将像“010011101”这样的二进制字符串转换为 int,以及如何在 C 中将像 5 这样的 int 转换为字符串“101”?
回答by Pointy
The strtolfunction in the standard library takes a "base" parameter, which in this case would be 2.
的strtol标准库函数采用一个“基地”参数,在这种情况下将是2。
int fromBinary(const char *s) {
return (int) strtol(s, NULL, 2);
}
(first C code I've written in about 8 years :-)
(我写了大约 8 年的第一个 C 代码 :-)
回答by Winder
If it is a homework problem they probably want you to implement strtol, you would have a loop something like this:
如果这是他们可能希望您实施的家庭作业问题strtol,您将有一个这样的循环:
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;
回答by Chris
I guess it really depends on some questions about your strings/program. If, for example, you knew your number wouldn't be bigger than 255 (IE you were only using 8 bits or 8 0s/1s), you could create a function where you hand it 8 bits from your string, traverse it and add to a sum that you returned everytime you hit a 1. IE if you hit the bit for 2^7 add 128 and the next bit you hit was 2^4 add 16.
我想这真的取决于关于你的字符串/程序的一些问题。例如,如果您知道您的数字不会大于 255(即您只使用 8 位或 8 个 0s/1s),您可以创建一个函数,将字符串中的 8 位传递给它,遍历它并添加到您每次命中 1 时返回的总和。IE 如果您命中 2^7 的位加 128,而您命中的下一位是 2^4 加 16。
This is my quick and dirty idea. I think more and Google for ya while at school. :D
这是我快速而肮脏的想法。在学校时,我会为你考虑更多和谷歌。:D
回答by Arun
For the 2nd part of the question, i.e. "how do I convert an int, like 5, to a string "101" in C?", try something like:
对于问题的第二部分,即“如何在 C 中将 int(例如 5)转换为字符串“101”?”,请尝试以下操作:
void
ltostr( unsigned long x, char * s, size_t n )
{
assert( s );
assert( n > 0 );
memset( s, 0, n );
int pos = n - 2;
while( x && (pos >= 0) )
{
s[ pos-- ] = (x & 0x1) ? '1' : '0'; // Check LSb of x
x >>= 1;
}
}
回答by muruga
You can use the following coding
您可以使用以下编码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
int nRC = 0;
int nCurVal = 1;
int sum = 0;
char inputArray[9];
memset(inputArray,0,9);
scanf("%s", inputArray);
// now walk the array:
int nPos = strlen(inputArray)-1;
while(nPos >= 0)
{
if( inputArray[nPos] == '1')
{
sum += nCurVal;
}
--nPos;
nCurVal *= 2;
}
printf( "%s converted to decimal is %d\n", inputArray, sum);
return nRC;
}
回答by user3689237
Use like this:
像这样使用:
char c[20];
int s=23;
itoa(s,c,2);
puts(c);
Output:
输出:
10111
回答by Alex Hoffmann
To answer the second part of the question.
回答问题的第二部分。
char* get_binary_string(uint16_t data, unsigned char sixteen_bit)
{
char* ret = NULL;
if(sixteen_bit) ret = (char*)malloc(sizeof(char) * 17);
else ret = (char*)malloc(sizeof(char) * 9);
if(ret == NULL) return NULL;
if(sixteen_bit){
for(int8_t i = 15; i >= 0; i--){
*(ret + i) = (char)((data & 1) + '0');
data >>= 1;
}
*(ret + 16) = '##代码##';
return ret;
}else{
for(int8_t i = 7; i >= 0; i--){
*(ret + i) = (char)((data & 1) + '0');
data >>= 1;
}
*(ret + 8) = '##代码##';
return ret;
}
return ret;
}

