解決此問題。謝謝你這麼多傢伙^^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";
你能告訴什麼輸入和預期輸出是十六進制什麼? – choroba 2013-05-07 15:20:23
0x89346512 - > 0x12653489 ---這就是我想要做的 – sflee 2013-05-08 00:09:52