How can I pack an int as a 32 bit big endian in Perl?

Consider this snippet:

use strict;
use warnings;

my $data = "1";
my $packed = pack("I",$data);
open(my $file,">","test.bin") || die "error $!\n";
binmode $file;
print $file $packed;

The fact is that, trying to read it from another language, this seems insignificant. Is there a template argument that allows me to write it as a big endian? I would like to avoid the extra work of reading.

+5
source share
2 answers

Consider using the "N" pattern with the package:

http://perldoc.perl.org/functions/pack.html

+4
source

The solution is a template N.

my $packed = pack "N", $data;

See the pack documentation for a list of all package options.

+3
source

All Articles