[Israel.pm] pack vs. different architectures

Levenglick Dov-RM07994 DovL at freescale.com
Tue Feb 1 04:55:42 PST 2005

Could this be an alignment issue? If so (and assuming gcc), you can circumvent this by specifying the alignment of the structure internals (http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/Variable-Attributes.html#Variable-Attributes) and of the struct itself (http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/Type-Attributes.html#Type-Attributes)

Best Regards,
Dov Levenglick
The information contained in this email is classified as:
[ ] General Business Information
[ ] Freescale Internal Use Only
[ ] Freescale Confidential Propriety
[x] Personal Memorandum

-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf Of Roman M. Parparov
Sent: Tuesday, February 01, 2005 2:29 PM
To: Perl in Israel
Subject: [Israel.pm] pack vs. different architectures

Hey, guys.

I am facing the following problem:
I have to write a C structure into a binary file using a perl program.
That file is later read by an external utility.
The perl program runs on an Origin200 (IRIX64 nasa 6.4 02121744 IP27 mips)
compiled using 'cc -32' (because of Oracle 7.3.4 present) - perl 5.8.5
The external utility runs on a Pentium (Linux earth 2.4.22 i686)

Please note that the irix is a 64-bit machine.

The structure is as following:
struct rpthdr {
  char  id[8];    /* Station ID */
  float lat;      /* Latitude of Station */
  float lon;      /* Longitude of Station */ 
  float t;        /* Time in grid-relative units */ 
  int   nlev;	  /* Number of levels following */ 
  int   flag;     /* Level independent var set flag */ 
} hdr;

I am trying to write it using the following:
	$hdr = {
	my $str =
	  pack('Z9',$hdr->{id}) .
	    pack('f',$hdr->{lat}) .
	      pack('f',$hdr->{lon}) .
		pack('f',$hdr->{t}) .
		  pack('I',$hdr->{nlev}) .

After this I need to write a sequence of numbers:
	for my $d (@data) {
In fact I am writing a sequence of such structures.

But I am still having problems reading the file afterwards with that
external utility! For example, if I pack the id string with Z8, I lose
the last character, and if I pack it with a8 - two additional characters
are read for some reason. The lat/lon fields are read incorrectly and the
second structure is already a total garbage.

I guess given these architectures, the packing must be less trivial...
Any advices?

Roman M. Parparov - NASA EOSDIS project node at TAU technical manager.
Email: romm at empire.tau.ac.il		   http://www.nasa.proj.ac.il/
Phone/Fax: +972-(0)3-6405205 (work),        +972-(0)50-734-18-34 (home)
The economy depends about as much on economists as the weather does on
weather forecasters.
                -- Jean-Paul Kauffmann
Perl mailing list
Perl at perl.org.il

More information about the Perl mailing list