Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^11: Bit handling in Perl

by BrowserUk (Patriarch)
on Oct 27, 2014 at 14:54 UTC ( [id://1105161]=note: print w/replies, xml ) Need Help??


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.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^12: Bit handling in Perl
by vaidhy_m (Novice) on Oct 31, 2014 at 09:28 UTC

    Dear BrowserUK, read worked flawlessly and I got what I wanted. Thanks a million to you and all the monks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1105161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found