I'm hoping to get a quick hand with my code. I need to convert char c
to binary before my function does other things. However, I am getting errors that I am using itoa
wrong and am not sure how else to even attempt this part of the function.
The main function is:
char *alpha = "abcde";
unsigned char bits[8];
for (int pos=0;pos<5;pos )
{
printf( "%c ", alpha[pos] );
char2bits( alpha[pos], bits );
}
The function, for what I have so far, is:
void char2bits( char c, unsigned char bits[8] ){
for(int i = 0; i < 5; i ){
itoa(c, (bits)[i], 2);
}
// ...
}
CodePudding user response:
A possible implementation of char2bits()
:
void char2bits(char c, unsigned char bits[8]) {
for (size_t i = 0; i < 8; i = 1) {
bits[i] = (c >> (7 - i)) & 1;
}
}
int main(void) {
char c = 3;
unsigned char bits[8];
char2bits(c, bits);
// now `bits` is equal to {0, 0, 0, 0, 0, 0, 1, 1}
for (size_t i = 0; i < 8; i = 1) {
printf("%hhu\n", bits[i]);
}
return 0;
}
CodePudding user response:
First when you want to work with bytes, you should use unsigned char or uint8_t.
This function change a byte into a printable string (you can use printf or puts with "bits" array):
void char2bits(unsigned char c, unsigned char bits[8]) {
int shift, i;
for(shift = 7, i = 0; i < 8; --shift, i)
bits[i] = ((c >> shift) & 0x1) '0';
}
What happend ?
with c = 250 = 1111 1010
first your bits table is empty so: bits = ""
Step i = 0:
(c >> shift) = shift right by 7 = 0000 0001
(c >> shift) & 0x1 = keep only last bit = 0000 0001
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 48 1 = 49 = '1' in ascii
so bits = "1";
Step i = 1:
(c >> shift) = shift right by 6 = 0000 0011
(c >> shift) & 0x1 = keep only last bit = 0000 0001
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 48 1 = 49 = '1' in ascii
so bits = "11";
Step i = 2:
(c >> shift) = shift right by 5 = 0000 0111
(c >> shift) & 0x1 = keep only last bit = 0000 0001
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 48 1 = 49 = '1' in ascii
so bits = "111";
Step i = 3:
(c >> shift) = shift right by 4 = 0000 1111
(c >> shift) & 0x1 = keep only last bit = 0000 0001
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 48 1 = 49 = '1' in ascii
so bits = "1111";
Step i = 4:
(c >> shift) = shift right by 3 = 0001 1111
(c >> shift) & 0x1 = keep only last bit = 0000 0001
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 48 1 = 49 = '1' in ascii
so bits = "11111";
Step i = 5:
(c >> shift) = shift right by 2 = 0011 1110
(c >> shift) & 0x1 = keep only last bit = 0000 0000
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 0 48 = 48 = '0' in ascii
so bits = "111110";
Step i = 6:
(c >> shift) = shift right by 1 = 0111 1101
(c >> shift) & 0x1 = keep only last bit = 0000 0001
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 1 48 = 49 = '1' in ascii
so bits = "1111101";
Step i = 7:
(c >> shift) = shift right by 0 = 1111 1010
(c >> shift) & 0x1 = keep only last bit = 0000 0000
((c >> shift) & 0x1) '0' = ((c >> shift) & 0x1) 48 = 0 48 = 48 = '0' in ascii
so bits = "11111010";
I hope that will help you.
CodePudding user response:
Here's how I would do it
void char2bits( char c, unsigned char bits[8] )
{
for( int i = 0; i < 8; i)
{
// create a mask for bit `i`
int mask = 1 << i;
// set the corresponding member of the `bits` array to 0 or 1
// depending on the value of the relevant bit
bits[i] = (c & mask) ? '1' : '0';
}
}
Hopefully this is clear.
The basic technique is to use the bitwise and &
operator to strip out a single bit from the input character. The mask will have values 1, 2, 4, 8 etc. as the loop executes. I feed the result of the &
into the ternary operator test ? a : b
to store either a "1" or a "0" character in the output array.
This is equivalent to writing out
if(c & mask)
bits[i] = '1';
else
bits[i] = '0';
Note that I've iterated over all 8 possible bits, rather than the 5 that your code used. Note, too, that in the output, bits[0]
will hold the least significant bit of the input character c
.