开发者

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

Consider this snippet:

use strict;
use warnings;

my $data = "1";
my $packed = pack("I",$data);
open(my $file,"&开发者_Go百科gt;","test.bin") || die "error $!\n";
binmode $file;
print $file $packed;

The thing is, trying to read it from another language, this appears to be little endian. Is there any template argument that allows me to write it as big endian? I'd like to avoid doing extra work when reading.


Consider using the "N" template with pack:

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


The solution is the N template.

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜