0

//i am new to programming, but i am trying to make a function or any method in c language that //would take a string and convert it a binary array. can anyone please help me with it, i made this but if i make all letters it will be very long**

char a[2] = "BA";
    char b[14];
    int j = 0;

    for(int i = 0; i <= 1; i++)
    {
    
        if ( a[i] == 'A' )
        {
        b[j] = '1';
        b[j+1] = '0';
        b[j+2] = '0';
        b[j+3] = '0';
        b[j+4] = '0';
        b[j+5] = '0';
        b[j+6] = '1';
        }

        if ( a[i] == 'B' )
        {
        b[j] = '1';
        b[j+1] = '0';
        b[j+2] = '0';
        b[j+3] = '0';
        b[j+4] = '0';
        b[j+5] = '1';
        b[j+6] = '0';
        }

        if ( a[i] == 'C' )
        {
        b[j] = '1';
        b[j+1] = '0';
        b[j+2] = '0';
        b[j+3] = '0';
        b[j+4] = '0';
        b[j+5] = '1';
        b[j+6] = '1';
        } 

    
        j = j+7;
    
    }

  • Does this answer your question? [Conversion of Char to Binary in C](https://stackoverflow.com/questions/7863499/conversion-of-char-to-binary-in-c) – Robert Harvey Feb 22 '22 at 18:54
  • Unrelated note: `char b[14];` has space for 13 *normal* characters and 1 zero terminator; you may want to do `char b[15];` if `b` is ever going to be treated as a "14-char" *string*. – pmg Feb 22 '22 at 19:14

0 Answers0