Home > Software design >  How can I change string into ASCII and then binary in C?
How can I change string into ASCII and then binary in C?

Time:10-03

So I'm working on a problem asking me to change an user input string to ASCII then binary in recursive way. I managed to do this for a single character, but when I try to use a for loop it throw me a weird result. Can anyone help me fix this?

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

int main()
{
    

            char toBS;
            printf("Enter the string you want to convert: \n"); 
            scanf("%c", &toBS);
            int i;
            for (i = 0; i<toBS 1; i  ){
            print(toBS);}
            printf("\n"); 
            return;
       }
        void print(char c)
        {
            int n = CHAR_BIT;
            binaryPrinter((unsigned char)c, &n);
            putchar('\n');
        }
        void binaryPrinter(int value, int *numberOfOnes)
        {
            if((*numberOfOnes)--){
                binaryPrinter(value >> 1, numberOfOnes);
                printf("%d", value & 1);
            }
        }

Here's my result:

Input: a

Output:

01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001

CodePudding user response:

Apart from lousy formatting, there are a few problems with your code:

scanf("%c", &toBS);
// will only get one character, not a string.
// and there is no testing to see if scanf() actually worked.
for (i = 0; i < toBS   1; i  ) { // counts up to the ASCII character's value.
    print(toBS);
}
void print(char c) { // sole purpose is to add one parameter to recursive function call
printf("%d", value & 1); // all the machinery of 'printf()' to output either '0' or '1'

It can be made much simpler if you think your way through what you want.

#include <stdio.h>
#include <limits.h>

void asBin( char c, int b ) {
    if( b ) {
        asBin( c, b-1 );
        putchar( '0'   (1&(c>>(CHAR_BIT-b))) );
        if( b == CHAR_BIT ) putchar( ' ' );
    }
}

int main() {
    for( char *str = "Hello!"; *str; str   )
        asBin( *str, CHAR_BIT );
    putchar( '\n' );

    return 0;
}
01001000 01100101 01101100 01101100 01101111 00100001

And even simpler if you apply the right method (iteration) instead of recursion.

#include <stdio.h>
#include <limits.h>

int main() {
    for( char *str = "Hello!"; *str; str   ) {
        for( int i = CHAR_BIT; i--;  )
            putchar( '0'   (1&(*str>>i)) );
        putchar( "\n "[str[1] != 0] );
    }
    return 0;
}
  •  Tags:  
  • c
  • Related