in reply to Re^10: Bit handling in Perl
in thread Bit handling in Perl
Your problem is that you are mixing binary and text operations on the same file.
When you are writing your records out, you are adding a "\n" at the end, and when you are reading them back you are relying upon readline to read back the appropriate number of characters by reading until it sees this newline.
The problem comes when one of your binary records contains a data byte that happens to look like an LF character (0x0a; decimal 10).
So, for example, in the third record of your sample, the 56th set of 8 1s&0s looks like this: 0,1,0,1,0,0,0,0, which when packed using the 'b*' template, gets translated in a byte value of 0x0a.
Then, when you read the binary record back using readline (aka. <C>), perl sees that byte value as the end of the record, hence everything after that is wrong.
The answer is (as hinted at in my first reply to you above) is to not use text operations (newline record separators and readline) on your binary files.
Instead, when reading your binary files, you should use read and the exact number of bytes to read each record.
Study the documentation for read.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^12: Bit handling in Perl
by vaidhy_m (Novice) on Oct 31, 2014 at 09:28 UTC |