Hi!
Here's my problem. I want to read a file and then I want to rotate the file's bits. I know how to rotate bits, but I do not know how to do this with files. Any ideas?
Hi!
Here's my problem. I want to read a file and then I want to rotate the file's bits. I know how to rotate bits, but I do not know how to do this with files. Any ideas?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Code:FILE *fp = fopen( "file","rb"); int ch; while ( (ch=fgetc(fp)) != EOF ) { // rotate ch }
Ok, it is working. But I'm trying to find out how to get back the original file. I rotated the file in left for 1 bit, and now if I want to get it back i rotated the file in right for 1 bit - it is not working. Why not?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
OK, I found the problem. The problem was that I used signed chars.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Because when you rotate say
xyyyyyyy
left one bit, what you write out the the file is
yyyyyyy0
You lost the 'x' bit
What you need to do is actually write out
yyyyyyyx
which you get from say
ch = (ch << 1) | ( (ch >> 7) & 0x01 )
Now I found out that if I rotate the file more than 1 time then it is all mixed up. I'll try your code Salem.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Salem
Please explain what your code means.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Ok, you start with a byte which looks like this
xyyyyyyy
To hide it's true value when you write it out to another file, you want it to look like this
yyyyyyyx
Ie, everything rotated one bit to the left (and the most significant bit wrapped around to the least significant bit)
To do this in C, we do this
ch = ( (ch << 1) & 0xFE ) | ( (ch >> 7) & 0x01 )
( (ch << 1) & 0xFE ) repositions all the y bits
( (ch >> 7) & 0x01 ) repositions the x bit
To get back to where we started, we have
yyyyyyyx
And we want to get to
xyyyyyyy
ch = ( (ch << 7) & 0x80 ) | ( (ch >> 1) & 0x7F )
( (ch << 7) & 0x80 ) repositions the x bit
( (ch >> 1) & 0x7F ) repositions all the y bits
THANK YOU Salem!
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)