If I have a character array
unsigned char charBuff[5] = {'a','b','c','d', '\0'};
and I want to obtain the 8 bit binary representation of each of the
characters, how do I do that?
To print the binary equivalent of each char of the array, you can follow below steps
Step-1 : Find the number of elements in the given char array
unsigned char charBuff[5] = {1,2,3,4};
unsigned int numElem = sizeof(charBuff) / sizeof(charBuff[0]);
Step-2 : Rotate a loop till numElem
times. For e.g
for(index = 0; index < numElem; index++)
{
}
Step-3: select each char array index & implement the functionality to get the binary equivalent of each char in the array.
for(index = 0; index < numElem; index++)
{
/* select charBuff[index] and charBuff[index] is a single char i.e it can only 8 bits hence rotate inner loop 8 times */
for(subIndex = 0; subIndex < 8; subIndex++)
{
printf("%d", (charBuff[index]) >> subIndex & 1); /* it prints 0 or 1 */
}
printf(" "); /* Optional : print white space for readability after printing binary equivalent of charBuff[index] */
}