Monday 15 August 2011

c - Storing the lowest level bits and returning a char -



c - Storing the lowest level bits and returning a char -

i'm working on assignment decodes secret message stored in ppm format picture. first thing need find length of message, hidden in single byte spread across first 8 info bytes of picture. after have this, logic create method have loop shifts bits 7 spots, records first one, , when there 8 bits, homecoming char represent. right i'm trying understand how work. trying value length of hidden message. tried manually see how bits behaved.

fgets(str,64,fp); char length[7]; length[0] = str[7]; length[1] = str[15]; length[2] = str[23]; length[3] = str[31]; length[4] = str[39]; length[5] = str[47]; length[6] = str[55]; length[7] = str[63]; printf(length);

so since length hidden in lowest level bit of 8 bytes used fgets(str,64,fp); , stored each 8th value in array. returns "enpm0dp". when changed array int, output arrow.

can explain me how store bits byte , homecoming char corresponds value? utilize array of 8 bits? or store them in string?

you seem think fgets() counts length in bits (64 8 bytes), doesn't; counts in characters. see the documentation.

next, seem think str ana rray of bits, arrays of bits don't exist in c.

you're going have consider array of characters, we'll assume bytes:

unsigned char str[8]; if(fgets(str, sizeof str, fp)) { unsigned char length = 0, i; for(i = 0; < sizeof str; ++i) { length >>= 1; length |= str[i] & 0x80; } }

c arrays bit-manipulation

No comments:

Post a Comment