ss-perl
ss-perl

Reputation: 237

How do I use "pack/unpack" in Perl?

My data is encoded as 64-bit network byte order when I parse it using the Ruby language as following:

def unpack_string(str)
   binary = str.unpack('m*').first
   binary.unpack('G*')
end

my_array_of_floats = unpack_string(str)

How could I do the same thing using Perl's pack/unpack?

Upvotes: 1

Views: 1998

Answers (1)

ikegami
ikegami

Reputation: 386706

Looking at the Ruby documentation,

  • m is MIME base64 encoding.
  • G is a double-precision, network (big-endian) byte order

Perl's unpack doesn't do base64, but MIME::Base64 does.

In Perl's unpack, d is a double precision. You can specify the endianness using >.

use MIME::Base64 qw( decode_base64 );
my @nums = unpack 'd>*', decode_base64 $str;

> was introduced in Perl 5.10.

Upvotes: 7

Related Questions