Hint: in order to convert a byte in binary format, you may, iteratively, test bit 7 and then shift left the number, e.g. iterate the following code
char binary[9];
binary[8] ='\0'; unsigned char a = 'a'; a is 0x61
binary[0] = (a & 0x80) ? '1' : '0';
a <<= 1;
binary[1] = (a & 0x80) ? '1' : '0';
a <<= 1;
the 'conversion process' code could also count the total number of collected zeroes (or ones).
[update]
Your
printStringAsBinary
doesn't iterate over characters. Try:
void printStringAsBinary(char* s)
{
while ( *s )
{
printCharAsBinary(*s);
printf("\n");
++s;
}
}
[/update]
[another update]
A modification of printCharAsBinary allows to count how many ones you have in the byte:
int printCharAsBinary(char c) {
int ones = 0;
int i;
for(i = 0; i < 8; i++)
{
char bit = (c & 0x80) ? 1 : 0;
printf("%c", bit+'0');
ones += bit;
c <<= 1;
}
printf("\n");
return ones;
}
[/another update]