How can I create a binary file in Perl?

后端 未结 3 1394
广开言路
广开言路 2020-12-30 19:20

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.

相关标签:
3条回答
  • 2020-12-30 20:02

    Yes, use binmode

    For your entertainment (if not education) my very first attempt at creating a binary file included binmode STDOUT and the following:

    sub output_word {
        $word = $_[0];
        $lsb  = $word % 256;
        $msb  = int($word/256);
        print OUT chr($lsb) . chr($msb);
        return $word;
    } 
    

    FOR PITY'S SAKE DON'T USE THIS CODE! It comes from a time when I didn't know any better.

    It could be argued I still don't, but it's reproduced here to show that you can control the order of the bytes, even with brain-dead methods, and because I need to 'fess up.

    A better method would be to use pack as Adam Batkin suggested.

    I think I committed the atrocity above in Perl 4. It was a long time ago. I wish I could forget it...

    0 讨论(0)
  • 2020-12-30 20:15

    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.

    0 讨论(0)
  • 2020-12-30 20:16

    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.

    0 讨论(0)
提交回复
热议问题