Perl:如何将big endian转换为little endian
问题描述:
解决此问题。谢谢你这么多家伙^^Perl:如何将big endian转换为little endian
我的问题,我使用下面陈述的解决方案。
原来的问题:---主编2013年5月8日
我知道我可以用C做这个任务++这样的:
struct { /* File Header */
int a;
int b;
short c;
short d;
} PPPhdr;
PPPhdr head;
fstream fst;
fst.open("file.txt", ios_base::in|ios_base::binary);
fst.read((char*)&head, sizeof(PPPhdr));
SwapInt32(&(head.a));
SwapInt32(&(head.b));
SwapShort(&(head.c));
SwapShort(&(head.d));
所以,基本上SwapInt32会做到这一点:
0x89346512 -> 0x12653489
SwapShort会做到这一点:
0x3487 -> 0x8734
现在我的问题是,我该如何在Perl中执行此操作?
我的方式:
open FH, "<file.txt" or die print "Cannot open file\n";
binmode FH;
read FH, $temp, 12;
($a,$b) = unpack("N2", substr($temp,0,8));
($c,$d) = unpack("n2", substr($temp,8,4));
close(FH);
print "$a\n$b\n$c\n$d\n";
答
你收拾它,并解压反过来:
print "ok\n" if 0x12653489 == unpack 'L', pack 'N', 0x89346512;
+0
非常感谢。 – sflee 2013-05-08 05:08:11
你能告诉什么输入和预期输出是十六进制什么? – choroba 2013-05-07 15:20:23
0x89346512 - > 0x12653489 ---这就是我想要做的 – sflee 2013-05-08 00:09:52