![](/img/trans.png)
[英]How to store the bit pattern of a 32 bit integer in a 32 length string of 0s and 1s in C
[英]How do I read 0s and 1s from text input file and perform bit manipulation (shifting)
我有一个文本文件输入,其中有两列,第一列具有二进制数,第二列具有字符串宽度(将在输出数据时使用)。 读取二进制输入后,我需要执行位操作。
我正在使用以下代码读取输入文件,并将input
声明为unsigned int
。
while ((fscanf(fp,"%s %d", input, &count) != EOF))
当我尝试执行位操作时接受输入后,我收到“无效的二进制操作数”错误消息。 所以我做任何声明错误,还是需要将二进制转换为十进制来执行位操作? 处理后,我需要将输出作为位序列发送到文件。
如果添加了输入文件示例,会更容易,但是首先需要正确输入。 我建议您首先尝试这样的事情:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char input[80];
int count;
int bin;
fp = fopen("infile.txt", "r");
while ((fscanf(fp,"%s %d", input, &count) != EOF)) {
bin = strtol(input, NULL, 2);
printf("input: %s, count %d, bin %d\n", input, count, bin);
}
fclose(fp);
return 0;
}
在名为“ infile.txt”的输入文件上看起来像这样:
0011101 10
1001011 20
0101011 30
1001010 40
这将给出以下输出:
input: 0011101, count 10, bin 29
input: 1001011, count 20, bin 75
input: 0101011, count 30, bin 43
input: 1001010, count 40, bin 74
因此,如果我走的路不正确,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.