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

How do I store a small binary file in a perl string?

Isn't there a more elegant way than inserting a hex qualifier in front of each digit? (like $mystring = "\x0D\x0A...")

thanks!

Replies are listed 'Best First'.
Re: how to store binary string?
by ikegami (Patriarch) on May 11, 2005 at 22:05 UTC

    You could do

    $mystring = pack('H*', '0D0A...');

    or

    # Ignores whitespace ($mystring = '0D 0A ...') =~ s/([0-9a-fA-F]{2})/chr($1)/g;

    or

    # Multiline and ignores whitespace ($mystring = <<'__EOI__') =~ s/([0-9a-fA-F]{2})/chr($1)/g; 0D 0A ... 0D 0A ... 0D 0A ... ... __EOI__
Re: how to store binary string?
by bart (Canon) on May 11, 2005 at 22:12 UTC
    You can use UUencoding, perl supports it natively (= without any modules). It can wrap nicely in your source code, too: the line lengths in UUencoded strings are limited.
    print unpack 'u*', '92G5S="!!;F]T:&5R(%!E<FP@2&%C:V5R+```';

    You can use perl to produce the UUE string, from a binary file, for example.

    print pack 'u*', $binstring;

    Likewise, you can use a hex string. It doesn't wrap nicely, not without extra measures anyway, and it's longer than UUE: 2x versus 4/3x increase in size.

    print pack 'H*', '4A75737420416E6F74686572205065726C204861636B65722C';
    Generating such a string can again be done in Perl.
    print unpack 'H*', $binstring;
    Note that, compared to UUE, the meaning of packand unpack are swapped: in hex, raw/binary is packed, hex text is unpacked. OTOH, UUE is packed, the raw original is unpacked.
Re: how to store binary string?
by rduke15 (Beadle) on May 11, 2005 at 22:41 UTC

    If it's in a file, you just read the file into the string and don't worry.

    From the perlfaq (perldoc -q "How can I read in an entire file all at once?"):

    { local(*INPUT, $/); open (INPUT, $file) || die "can't open $file: $!"; binmode INPUT; # needed if you are on Windows $var = <INPUT>; }
      You will avoid being biten by Unicode using binmode wherever you need to deal with a stream of bytes; from perldoc -f binmode:
      On some systems (in general, DOS and Windows-based systems) binmode() is necessary when you're not working with a text file. For the sake of porta- bility it is a good idea to always use it when appropriate, and to never use it when it isn't appropriate. Also, people can set their I/O to be by default UTF-8 encoded Unicode, not bytes. In other words: regardless of platform, use bin- mode() on binary data, like for example images.

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.
Re: how to store binary string?
by zentara (Cardinal) on May 12, 2005 at 10:25 UTC
    Along the same lines as pack, there is the MIME::Base64 module, a standard part of Perl.
    use MIME::Base64 qw( encode_base64 decode_base64); $ascii_dat = encode_base64($bin_string); #or reverse $bin_string_back = decode_base64($ascii_dat);

    I'm not really a human, but I play one on earth. flash japh