From: Eric Hodel Date: 2006-02-22T15:27:52+09:00 Subject: Re: Question about String.unpack On Feb 21, 2006, at 6:28 AM, Eric Jacoboni wrote: > Say i've a C struct like that: > > typedef struct { > char name[30]; > double size; > int age; > char stuff; > } enreg_t; > > This record is written in a file that i want to re-read with a Ruby > script (just a toy example... to understand). > > I've not found a "clean" way to unpack such a record... > > My C compiler gives me a total sizeof of 48, so i use a > fd.sysread(48).unpack(A32dIC) to re-read. But that seems rather Your struct says char name[30] not 32... > 'tricky' (i suppose we don't have always such a > information). Furthermore, the first element is completed with > garbage if the name field have less than 30 "useful" chars ("Doe", > for example). Did you initialize the C struct correctly? You should zero out all of name. > I'm sure i'm missing something but i've not managed to find the right > way to do that. -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com