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);
}
j2l3ks. Maybe it is a simple as that you are enteringj2l3ks\norj2l3ks\0. Further, your binary prints the least significant bit first.printf("The character %x %x '%c' =", (unsigned char) c, (unsigned char) charToInt(c), c);and report results. I suspect mischief inbinaryPrinter(8, charToInt(c), &count), specificallycharToInt(char c). Is coded posted exactly?