0

I am writing a program where I need to convert Ascii characters to binary and then do a count. I have gotten my code working but it is printing additional information and not necessarily the correct binary. Below is my code as well as the output for a given set of characters. Any assistance would be greatly appreciated!

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

    void binaryPrinter(int digEnd, int value, int * noOfOnes);
    void print(char c);

    int charToInt(char c)
    {
        return (int) c;
    }

    int main()
    {
        char value;
        int result = 1;
        while(result != EOF)
    {
        result = scanf("%c", &value);
        if(result != EOF)
        {
            print(value);
        }
    }
 }

    void binaryPrinter(int digEnd, int value, int * noOfOnes)
    {
        if(value & 1)
        {
        (*noOfOnes) = (*noOfOnes) + 1;
        value = value >> 1;
        digEnd--;
        printf("1");
        }
    else
        {
        value = value >> 1;
        digEnd--;
        printf("0");
        }

    if(digEnd == 0)
        return;
    else
        binaryPrinter(digEnd, value, noOfOnes);
    }

    void print(char c)
        {
        int count = 0;
        printf("The character %c =", c);
        binaryPrinter(8, charToInt(c), &count);
        printf(" 1's = %d\n", count);
        }
5
  • The output: (also notice the last line - I believe it is printing the null value but am not sure how to remove it) j2l3ks The character j =10101001 1's = 4 The character 2 =10110011 1's = 5 The character l =11001001 1's = 4 The character 3 =00110011 1's = 4 The character k =00101001 1's = 3 The character s =00110001 1's = 3 The character =10101111 1's = 6 Commented Jun 26, 2014 at 0:27
  • You mention ASCII. It is unlikely that your stdin will provide only ASCII characters or your program will work with only ASCII characters. Many characters sets are supersets of ASCII. And many encodings are identical to ASCII for ASCII characters. Your program prints, in binary, the byte values of encoded characters. It doesn't care or know the character set nor encoding. They are likely to be Unicode/UTF-8 or something like Windows-1252. Copy and paste this character into your terminal to see what your program outputs. Commented Jun 26, 2014 at 2:06
  • Explain how you are entering your input of j2l3ks. Maybe it is a simple as that you are entering j2l3ks\n or j2l3ks\0. Further, your binary prints the least significant bit first. Commented Jun 26, 2014 at 2:15
  • I used the ƛ character and received this output - The character _ = 01000111 1's = 4 The character _ = 01001101 1's = 4 The character _ = 01101001 1's = 4 The character = 01010000 1's = 2 Commented Jun 26, 2014 at 2:22
  • There certainly is a mystery here - the last code printed differs in your 2 examples. Suggest printf("The character %x %x '%c' =", (unsigned char) c, (unsigned char) charToInt(c), c); and report results. I suspect mischief in binaryPrinter(8, charToInt(c), &count), specifically charToInt(char c). Is coded posted exactly? Commented Jun 26, 2014 at 4:49

1 Answer 1

2

Here's a pair of functions:

void printCharAsBinary(char c) {
    int i;
    for(i = 0; i < 8; i++){
        printf("%d", (c >> i) & 0x1);
    }
}

void printStringAsBinary(char* s){
    for(; *s; s++){
        printCharAsBinary(*s);
        printf(" ");
    }
    printf("\n");
}

You can see them in action here: http://ideone.com/3mEVbE. They work by masking out a single bit of each character and printing one at a time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.