Using Bitwise Ops to Decode Message

// The output is showing: BAACDALMEGFOLPPHCDBMDGAEDPHBA which is obviously
// not a deciphered message. I know the first word should be: 'BE' with a
// space after. Can anyone see the issue?


#include <iostream>
using namespace std;

unsigned char bytes[25] = { 9, 0, 207, 176, 159, 163, 255, 33, 58,
115, 199, 255, 255, 181, 223, 67, 102,
69, 173, 6, 35, 103, 245, 160, 164 },

characters[27] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' };

unsigned short codeKey[27] = { 00000, 00001, 00010, 00011, 00100, 00101, 00110, 00111, 01000,
01001, 01010, 01011, 01100, 01101, 01110, 01111, 10000, 10001,
10010, 10011, 10100, 10101, 10110, 10111, 11000, 11001, 11111 };

int main() {

unsigned short buffer,
tempBuffer,
bits = 0,
i = 0;

while( i < 25 ){

if (bits < 5){
buffer <<= 8;
buffer |= bytes[i];
bits += 8;
tempBuffer = buffer >> ( bits - 5 );
}
else
tempBuffer = buffer >> (bits - 5);

int c = tempBuffer & 0000000000011111;

for( unsigned i = 0; i < 25; ++i ){
if( c == codeKey[i] )
cout << characters[i];
}

bits -= 5;
if( bits < 5 )
++i;
}

cout << endl << endl;

return 0;
}
If you are able to describe the algorithm, someone can check if it's been encoded correctly.
Here's the right code:

#include <iostream>
#include <cmath>
using namespace std;



unsigned char bytes[25] = { 9, 0, 207, 176, 159, 163, 255, 33, 58,
115, 199, 255, 255, 181, 223, 67, 102,
69, 173, 6, 35, 103, 245, 160, 164 },

characters[31] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ',
' ', ' ', ' ', ' '};

int main() {
unsigned short buffer,
bits = 0;
unsigned int i = 0;

while( i < 25 ){

if (bits < 5){
buffer <<= 8;
buffer |= bytes[i];
bits += 8;
}

int tempBuffer = buffer >> ( bits - 5 ) & 0x1f;

bits -= 5;
cout << characters[tempBuffer];

if( bits < 5 ) ++i;
}

cout << endl << endl;

return 0;
}
Topic archived. No new replies allowed.