YoDar
YoDar

Reputation: 387

Unpack a binary file to a text file - A problem that flips the values

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 ?

Upvotes: 1

Views: 778

Answers (2)

Brian Agnew
Brian Agnew

Reputation: 272427

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 ?

Upvotes: 3

ysth
ysth

Reputation: 98508

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

Upvotes: 4

Related Questions