This problem is solved. Thank you so much guys^^
My problem and the solution I am using is stated below.
Original problem: --- Edited 2013-05-08
I know that I can do this task by C++ like this:
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));
So, basically SwapInt32 will do this:
0x89346512 -> 0x12653489
SwapShort will do this:
0x3487 -> 0x8734
Now my question is, how can I do this in Perl?
My way:
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";
You say your data is big-endian, but you are using the i template (a signed integer value) in the unpack call. You should be using N (an unsigned 32-bit big-endian number). You may want to read the documentation.
Perl doesn't have a single-character format for a signed big-endian integer. Use pack 'i>' for that. (This requires at least Perl 5.10.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With