English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm having a problem reading the characters I broke down into bits. Basically I'm trying to break the the character array down bitwise and put it back together (this is all part of a grander project I'm working on). I've discovered that it seems to be writing fine, but converting it back into a character string has given me complications.

int main() {
char input[10], output[10];
int bits[10][7];

cout<<"What 10 character string should I learn? ";
cin>>input;

for(int i=0; i<10; i++) {
for(int j=0; j<7; j++) {
bits[i][j] = input[i] & 1;
input[i] = input[i] >> 1;
}
}

for(int y=0; y<10; y++) {
for(int u=0; u<7; u++) {
output[y] = output[y] | bits[y][u];
output[y] = output[y] << 1;
}
}

for(int h=0; h<10; h++) {
cout< }
return 0;
}

Any suggestions? Thanks.

2007-02-14 16:54:44 · 1 answers · asked by Harb Frame 3 in Computers & Internet Programming & Design

1 answers

to start with your bits[10][7] are defined as integers, make them char also. they should be 8 bits long instead of 7, but just dealing with ASCII characters it shouldnt matter.

when you break down the character into bits you are saving the LSB to bits[i][0], the next to bits[i][1], ... the MSB to bits[i][6].

when you rebuild the character from bits, bits[i][0] (LSB) ends up in the MSB position, and bits[i][6] (MSB) ends up in the LSB position.

how to fix? well, first I would make the bits array = to bits[10][8], and your "for" loop = for (int j = 0; j < 8; j++).

next, when you rebuild the character, you need to start from the MSB and fill to the LSB.
to do that you could do this:

for (int y = 0; y < 10; y++)
{
__for (int u = 7; U >= 0; u--)
__{
____output[y] = output[y] | bits[y][u];
____output[y] = output[y] << 1;
__}
}

2007-02-15 01:11:58 · answer #1 · answered by justme 7 · 0 0

fedest.com, questions and answers