I am trying to convert a character to its binary representation (so character –> ascii hex –> binary).
I know to do that I need to shift and AND. However, my code is not working for some reason.
Here is what I have. *temp points to an index in a C string.
char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
char x = *temp;
c = (x >> i) & 1;
printf("%d\n", c);
temp--;
}
We show up two functions that prints a SINGLE character to binary.
printbinchar(10) will write into the console
itoa is a library function that converts a single integer value to a string with the specified base.
For example… itoa(1341, output, 10) will write in output string “1341”.
And of course itoa(9, output, 2) will write in the output string “1001”.
The next function will print into the standard output the full binary representation of a character, that is, it will print all 8 bits, also if the higher bits are zero.
printbincharpad(10) will write into the console
Now i present a function that prints out an entire string (without last null character).
Consider however that itoa don’t adds padding zeroes, so printstringasbinary(“AB1”) will print something like: