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

Converting decimal array to binary string array with calloc

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

# 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?我能按照我尝试的方式写入单个字符吗?目前它给我一个段错误。

您需要为 字符串指针数组 以及 字符串本身 分配 space。第一次调用 calloc() 分配 指向字符串的指针数组 并且每个 malloc() 分配字符串本身:

# 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;
    int i;

    // Allocate an array of numLines pointers to strings
    char **binarystrings;
    if ( (binarystrings = calloc(numLines, sizeof(*binarystrings))) == NULL )
      return EXIT_FAILURE;

    // Allocate space for each string
    // binaryLength bits plus one for null terminator
    for ( i=0; i<numLines; ++i ) {
      if ( (binarystrings[i] = malloc((binaryLength + 1)*sizeof(**binarystrings))) == NULL )
        return EXIT_FAILURE;
    }

    // convert decimal array to 32-bit binary string array
    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] >> (31 - c);    
        if (k & 1)
            binarystrings[0][c] = '1';
        else
            binarystrings[0][c] = '0';
    }
    binarystrings[0][32] = '[=10=]';
}

此外,您没有在字符串末尾写入空终止符。当您尝试写入它 (printf("\n%s", binarystrings[0]);) 时,它会继续读取内存,越过您希望结束的位置。在末尾写入一个零 ('\0') 需要分配一个额外的字符,因此 malloc() 调用中的 +1

注意 sizeof() 运算符的使用。你不应该硬编码内存大小。在 C 中,让编译器弄清楚事情有多大,不要试图去猜测。指针在 32 位系统上可以是 4 个字节,在 64 位系统上可以是 8 个字节。即使您知道自己拥有的系统及其大小,也请使用 sizeof().

编辑:更正了@DavidCRankin 注意到的逻辑错误:

        k = decimals[0] >> (31 - c);    

抱歉,我的理解是您想将多个数字传递给您的函数并将它们全部转换为二进制字符串并返回。不管怎样,这个例子仍然有效。在任何一种情况下,要将 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