使用 calloc 将十进制数组转换为二进制字符串数组

标签 c arrays string binary calloc

我遗漏了一些代码,但基本上我有一个小数数组,我试图将它转换为二进制数,但作为一个字符串数组。 C 语言的新手,现在真的在抓紧救命稻草。我非常不确定 malloc 和 calloc 是如何使用的。这是我试图让它为第一个数字/二进制字符串工作的尝试:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

void binstringconvert (unsigned long int *decimals, char **binarystrings);


int main (int argc, char **argv) 
{
    // initialize variables
    int numLines = 9;
    int binaryLength = 32;
    unsigned long int decimals[numLines];   
    decimals[0] = 3241580200;   

    // convert decimal array to 32-bit binary string array
    char **binarystrings = calloc (numLines, binaryLength);
    binstringconvert(decimals, binarystrings);  

    // test print
    printf("\n\n%lu in binary number system is: ", decimals[0]);
    printf("\n%s", binarystrings[0]);   
}

void binstringconvert (unsigned long int *decimals, char **binarystrings)
{
    int c, k;

    for (c = 31; c >= 0; c--)
    {
        k = decimals[0] >> c;    
        if (k & 1)
            binarystrings[0][c] = '1';
        else
            binarystrings[0][c] = '0';
    }       
}

我是否正确初始化了 binarystrings?我能按照我尝试的方式写入单个字符吗?目前它给我一个段错误。

最佳答案

抱歉,我的解释是您想将多个数字传递给您的函数并将它们全部转换为二进制字符串并返回。不管怎样,这个例子仍然有效。在任何一种情况下,要将 32 位数字放入字符串中,您都需要 33 个字符(+1 表示 nul-terminating 字符)。此外,您正在以相反的顺序编写二进制字符串。

只需稍作调整即可纠正顺序。示例:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

enum { DWRD = 32 };

void binstringconvert (unsigned *decimals, char (*binarystrings)[DWRD+1], int n);

int main (void)
{
    /* initialize variables */
    unsigned int decimals[] = { 1, 255, 65535, 8388607,
                                3241580200, 2898560974,
                                4294967295, 3097295382,
                                1076482445, 1234567890 };
    char (*binarystrings)[DWRD+1] = {NULL};
    int i, n = sizeof decimals/sizeof *decimals;

    binarystrings = calloc (n, sizeof *binarystrings);
    binstringconvert (decimals, binarystrings, n);

    /* test print */
    for (i = 0; i < n; i++)
        printf (" %10u : %s\n", decimals[i], binarystrings[i]);

    free (binarystrings);

    return 0;
}

void binstringconvert (unsigned *decimals, char (*binarystrings)[DWRD+1], int n)
{
    int c, i, k;
    for (i = 0; i < n; i++) {
        for (c = 31; c >= 0; c--)
        {
            k = decimals[i] >> c;
            if (k & 1)
                binarystrings[i][31-c] = '1';
            else
                binarystrings[i][31-c] = '0';
        }
        binarystrings[i][DWRD] = 0;  /* nul-terminate */
    }
}

输出

$ ./bin/binstrings
          1 : 00000000000000000000000000000001
        255 : 00000000000000000000000011111111
      65535 : 00000000000000001111111111111111
    8388607 : 00000000011111111111111111111111
 3241580200 : 11000001001101101001011010101000
 2898560974 : 10101100110001001000011111001110
 4294967295 : 11111111111111111111111111111111
 3097295382 : 10111000100111001111101000010110
 1076482445 : 01000000001010011101000110001101
 1234567890 : 01001001100101100000001011010010

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

相关文章:

c - 访问超出 malloc() 的内存

java - 计算 Java 数组中的非重复匹配对

java - 将XML转换为JAVA字符串

c++ - 从函数返回指向全局数组的指针

objective-c - NSTask 调用命令top。获取错误

javascript - 打印文件中的数组数组

javascript - 如何在表单提交时添加嵌套对象?

javascript - 如何比较返回 false 或 true 的两个变量的第一个和最后一个字母

string - 将独特的组合映射到数字

c++ - 按特定值对结构成员进行排序