Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

how to read binary file and give a binary output?

by perllearn (Initiate)
on Oct 16, 2014 at 12:44 UTC ( [id://1104041]=perlquestion: print w/replies, xml ) Need Help??

perllearn has asked for the wisdom of the Perl Monks concerning the following question:

hi all, i want to read a binary file, do some operations in it (i want to ZERO all the value before 0x4fff and then preserve all the values after it ) and then output a modified binary file with ". bin" extension. i am a beginner in perl, could anyone of you help me do this as soon as possible?
  • Comment on how to read binary file and give a binary output?

Replies are listed 'Best First'.
Re: how to read binary file and give a binary output?
by choroba (Cardinal) on Oct 16, 2014 at 12:54 UTC
    Hi perllearn, welcome to the Monastery!

    You should check pack and unpack on how to handle binary strings. open or sysopen should show you how to open a file.

    as soon as possible
    It depends on how fast you can read and understand.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      binmode $filehandle; is also important.

      Don't forget index to find the byte sequence.

      McA

Re: how to read binary file and give a binary output?
by mr_mischief (Monsignor) on Oct 16, 2014 at 14:30 UTC
Re: how to read binary file and give a binary output?
by Loops (Curate) on Oct 16, 2014 at 17:18 UTC

    It's better if you can work out how to do this on your own by reading the sources mentioned above. Here is some example code, although much of it may still be confusing without doing some additional reading:

    # Set the input and output filenames my $ifilename = 'file.in'; my $ofilename = $ifilename . '.bin'; # Open the files and ensure we handle them as binary data open my $ifile, '<', $ifilename or die "could not open input file $!"; open my $ofile, '>', $ofilename or die "could not open output file $!" +; binmode $ifile; binmode $ofile; # Read the binary data $_ = do { local $/; <$ifile> }; # Replace the initial bytes with zero s/^(.*?)(\x4F\xFF)/"\x0" x length($1) . $2/se; # Store the result in the output file print $ofile $_; # Tidy up close $ifile; close $ofile;

      This makes the assumption that the file fits in RAM. This is usually, but not always, a safe assumption. Things may get complicated if that's not the case. Since we're not talking about line-oriented text files read (and possibly sysread and syswrite) would be good additional reading.

      If there are fixed-length records there are ways to deal with that. If there are record terminators consistent throughout the file, there's a way to deal with that, too. See $/ for more information.

        Good point about it all having to fit into RAM. Should be relatively straightforward to read byte by byte, although looking ahead for the two byte separator would need some finagling. hmmm, how about:

        my $ifilename = 'file.in'; my $ofilename = $ifilename . '.bin'; open my $ifile, '<', $ifilename or die "could not open input file $!"; open my $ofile, '>', $ofilename or die "could not open output file $!" +; binmode $ifile; binmode $ofile; my $n = eof($ifile) ? "" : getc($ifile); while ((my $c = $n) ne "") { $n = eof($ifile) ? "" : getc($ifile); print $ofile "$c$n" eq "\x4F\xFF" .. 1 ? $c : "\x0"; } close $ifile; close $ofile;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104041]
Approved by marto
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found