这个问题已经解决了。谢谢大家^^
以下是我的问题及解决方法。
原始问题:--- 编辑于2013-05-08
我知道可以使用 C++ 来完成这个任务,代码如下:
所以,基本上SwapInt32会执行以下操作:
现在我的问题是,如何用Perl实现这个?
我的方法:
以下是我的问题及解决方法。
原始问题:--- 编辑于2013-05-08
我知道可以使用 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";