Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a binary file in Perl?

For example, I want to create a file called sample.bin and put a number, like 255, so that 255 is saved in the file as little-endian, FF 00. Or 3826 to F2 0E.

I tried using binmode, as the perldoc said.

like image 293
domlao Avatar asked Sep 23 '09 06:09

domlao


People also ask

How do I create a binary file in Perl?

use strict; use warnings; use IO::File; use Convert::Binary::C; my $c = Convert::Binary::C->new('ByteOrder' => 'LittleEndian'); my $packed = $c->pack( 'short int', 0xFF ); print $packed; my $fh = IO::File->new( 'outfile', '>' ) or die "Unable to open outfile - $!\ n"; $fh->binmode; $fh->print( $packed );

How do you write a binary file?

To write to a binary fileUse the WriteAllBytes method, supplying the file path and name and the bytes to be written. This example appends the data array CustomerData to the file named CollectedData. dat .


2 Answers

The Perl pack function will return "binary" data according to a template.

open(my $out, '>:raw', 'sample.bin') or die "Unable to open: $!"; print $out pack('s<', 255); close($out); 

In the above example, the 's' tells it to output a short (16 bits), and the '<' forces it to little-endian mode.

In addition, ':raw' in the call to open tells it to put the filehandle into binary mode on platforms where that matters (it is equivalent to using binmode). The PerlIO manual page has a little more information on doing I/O in different formats.

like image 52
Adam Batkin Avatar answered Sep 17 '22 15:09

Adam Batkin


You can use pack to generate your binary data. For complex structures, Convert::Binary::C is particularly nice.

CBC parses C header files (either from a directory or from a variable in your script). It uses the information from the headers to pack or unpack binary data.

Of course, if you want to use this module, it helps to know some C.

CBC gives you the ability to specify the endianness and sizes for your C types, and you can even specify functions to convert between native Perl types and the data in the binary file. I've used this feature to handle encoding and decoding fixed point numbers.

For your very basic example you'd use:

use strict; use warnings;  use IO::File;   use Convert::Binary::C;  my $c = Convert::Binary::C->new('ByteOrder' => 'LittleEndian');  my $packed = $c->pack( 'short int', 0xFF );  print $packed;  my $fh = IO::File->new( 'outfile', '>' )    or die "Unable to open outfile - $!\n";  $fh->binmode;  $fh->print( $packed ); 

CBC doesn't really get to shine in this example, since it is just working with a single short int. If you need to handle complex structures that may have typedefs pulled from several different C headers, you will be very happy to have this tool on hand.

Since you are new to Perl, I'll suggest that you always use stict and use warnings. Also, you can use diagnostics to get more detailed explanations for error messages. Both this site and Perlmonks have lots of good information for beginners and many very smart, skilled people willing to help you.

BTW, if you decide to go the pack route, check out the pack tutorial, it helps clarify the somewhat mystifying pack documentation.

like image 37
daotoad Avatar answered Sep 19 '22 15:09

daotoad