Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Translate big endian C to perl on x86 of binary file into structure

by sg (Pilgrim)
on Sep 14, 2007 at 20:32 UTC ( #639072=perlquestion: print w/replies, xml ) Need Help??

sg has asked for the wisdom of the Perl Monks concerning the following question:

The following code is meant for a Big-endian processor running linux. The code reads binary files and assigns values to C-structures. The same data files need to be read in perl in Windows on little endian x86. Any tips on doing the translation?
typedef struct s_str_boo { unsigned short type; int x; int y; int w; int h; 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);
  • Comment on Translate big endian C to perl on x86 of binary file into structure
  • Download Code

Replies are listed 'Best First'.
Re: Translate big endian C to perl on x86 of binary file into structure
by ikegami (Patriarch) on Sep 14, 2007 at 20:38 UTC

    Convert::Binary::C might be useful. It'll help with not just your endianess issues, but your alignment issues.

Re: Translate big endian C to perl on x86 of binary file into structure
by renodino (Curate) on Sep 14, 2007 at 22:10 UTC
    See pack and unpack, esp. the N/n vs. V/v format specifiers. However, if you need to unpack floats, I fear you'll need to get out the IEEE spec and swizzle your own bytes.

    Perl Contrarian & SQL fanboy

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://639072]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2022-12-05 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?