Search code examples
carraysstringbinarycalloc

Converting decimal array to binary string array with calloc


I left some of the code out, but basically I have an array of decimals and I'm trying to convert it to binary numbers, but as an array of strings. Brand new to C and really grasping at straws at the moment. I'm very unsure of how malloc and calloc are used. This is my attempt at just trying to get it to work for the first number/binary string:

# 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';
    }       
}

Did I initialize binarystrings properly? Am I able to write to the individual characters the way I attempted to? At the moment it's giving me a segfault.


Solution

  • I apologize, I interpreted that you wanted to pass multiple numbers to your function and have them all converted to binary strings and returned. No matter, the example still works. In either case, to fit a 32-bit number into a string you will need 33-characters (+1 for the nul-terminating character). Additionally, you are writing your binary string in reverse order.

    Just a few tweaks will correct the order. Example:

    # 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 */
        }
    }
    

    Output

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