2
votes

I'm trying to convert ASCII strings into Binary so i can add Parity to it (Hamming Code). But the output is not right at all.

If i enter 'A' on the input it should return: 01000001 and i've tried it with 'B' but it doesn't return that

unsigned char bits[8];
for(int i = 8; i >= 1; i--){

i expect the output to be 01000001 for 'A' but the actual output is 00100000 Same for ABC or B or C

1
upper bit is 7, lower bit is 0. you're shifting too much with your for loop - B. Go

1 Answers

0
votes

i is off by one, so you are accessing elements bitC[8] to bitC[1] (even though bitC[8] is out of bounds), and you are printing bits 8 to 1 instead of bits 7 to 0.

Replace

for(int i = 8; i >= 1; i--){

with

for(int i = 8; i--; ){