将二进制字符串转换为整数

标签 c int strtol

当我尝试将我的二进制字符串转换为 int 时,我收到了一些我无法弄清楚的错误。首先,我正在从一个文件中读取数据,当我转换时,前导零没有出现,新行显示为零。

我在这个问题中使用的代码:Convert binary string to hexadecimal string C

char* binaryString[100];

// convert binary string to integer
int value = (int)strtol(binaryString, NULL, 2);

//output string as int
printf("%i \n",value)

我的 txt 文件和我期望的输出:

00000000

000000010001001000111010
00000000000000000000000000000001
101010111100110100110001001001000101

我得到的:

0
0
70202
1
-1127017915

最佳答案

这一行:

char* binaryString[100];

正在声明一个包含 100 个字符指针(或 100 个字符串)的数组。你的意思可能是声明一个 100 个字符的缓冲区被解释为单个字符串:

char binaryString[100];

关于将二进制字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54507677/

相关文章:

c - token to unsigned printing odd 值

ios - 调用 `errno` 后测试 `strtol` 返回 "No such process"

c++ - 在 C++ 中使用 extern 时出错

c - 使用 printf 按小数点对齐 double 不起作用

mysql - 从 'float' 转换为 'int' ?

math.floor : int too large to convert to float 的 Python 问题

c - 将带有数字的字符串存储为整数数组

c - 为什么我无法使用 PEM_read_RSAPublicKey 读取 openssl 生成的 RSA 公钥?

c - 为什么我的内核没有超过共享内存限制?

c++ - 对整数的多维 vector 进行排序?