tags:

views:

134

answers:

2

Hello friends,

How can I unpack a 4byte binary file, store like the following example, to array or TEXT file ?

input file:

00000000  00 00 00 00 00 00 00 01  00 00 00 01 00 00 00 00  |................|
00000001  00 00 00 01 00 00 00 01  00 00 00 01 00 00 00 01  |................|

desired output file:

0,1,1,0,1,1,1,1

For now I'm using the following unpack code:

open(ERROR_ID_BIN, "<", "/error_id.bin") or die $!;
local $/;
my @err_values = unpack("V*", <ERROR_ID_BIN>); 
close(ERROR_ID_BIN);
print "\n\n\n\n\t@err_values\n\n\n";

And my problem is that it flips the values and gives me that:

0,16777216,16777216,0,16777216,16777216,16777216,16777216

What should I do ?

+4  A: 

V is little-endian (least significant byte first); try N for big-endian (most significant byte first).

ysth
yeah, I guess that fix the mess! thanks ysth
YoDar
Clox
+3  A: 

From the pack documentation

N An unsigned long (32-bit) in "network" (big-endian) order.

V An unsigned short (32-bit) in "VAX" (little-endian) order.

Don't you want 'N' to correct your endness ?

Brian Agnew
Yes, 'N' change the result to the expected result.
YoDar