Suresh Govindachar wrote:
> Hello,
>
> The following code is meant for a Big-endian processor running
> linux. The code reads binary files and assigns values to
> structures. I would like to read the same data files in perl on
> Windows on little endian x86. Any tips on doing the translation?
I would just use unpack - you could write your own char-by-char conversion
routines if you wanted to (get_long, get_ulong, get_short, etc). Longs
and shorts are considered to be 32/16 bits and ints are based on build type,
but there are native options. Read the pack info in perlfunc man page.
sysopen IN, $file, O_RDONLY or die "open $file: $! ($^E)";
binmode IN;
my $bytes = sysread IN, $data, 4;
die "Wrong length data ($bytes): $! ($^E)" if $bytes != 4;
printf "data=%d\n", unpack 'N', $data; # convert network order long
close IN;
# or read the whole struct into $data and unpack it in one call:
my ($type, $x, $y, $w, ... ) = unpack 'nNNN...', $data;
> typedef struct s_str_boo
> {
> unsigned short type;
> int x;
> int y;
> int w;
> int h;
> int v1;
> int v2;
> unsigned short s1;
> unsigned short s2;
> } str_boo;
>
> typedef struct s_str_foo
> {
> unsigned int aui;
> str_boo boo[16];
> unsigned short aus;
> struct s_str_foo *next;
> } str_foo;
>
>
> file = fopen("foo.dat", "r+b");
>
> fread((void*)&an_uint, sizeof(char), sizeof(unsigned int), file);
> fread((void*)tmpfoo, sizeof(char), sizeof(str_foo), file);
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs