--- Mathieu Bouchard <matju / sympatico.ca> wrote:
> On Tue, 10 Jul 2001, Al Chou wrote:
> 
> > typedef struct all_thing {
> > 	uint32_t	a;		
> > 	uint8_t		b;	
> > 	uint8_t		c;	
> > 	int8_t		d;	
> > 	uint8_t		e;	
> > 	uint32_t	f;	
> > 	uint64_t	g;	
> > 	char_t		data[0];	
> > } thing;
> > where the final member is an arbitrarily long sequence of characters.
> > Do I need to convert my data to a string of ASCII characters in order to
> > write this struct to a socket? 
> 
> 
> 
> Into a String, yes, but Strings contain arbitrary data first and foremost,
> and then only you have methods that support ASCII.
> 
> You can use Array#pack like this, to write an array of those values onto a
> stream:
> 
> # 64bit hack assuming little-endian
> foo.write([a,b,c,d,e,f,g&(1<<32-1),g>>32,data].pack("LCCcCLLLa*"))
> 
> 
> 
> Or you can use RubyX11's Type.rb to describe a struct like this:
> 
> class AllThing<Tuple; fields_are \
>   [:a, Uint32],		
>   [:b, Uint8],	
>   [:c, Uint8],
>   [:d, Int8],	
>   [:e, Uint8],
>   [:f, Uint32],
>   [:g, Uint64],
>   [:data, String8]
> end	
> 
> and then:
> 
> bar = AllThing.new(a,b,c,d,e,f,g,data)
> bar[0] # returns a
> bar.a  # returns a
> bar.class.xwrite(bar)
> 
> except you'll have to define Uint64 once beforehand (should take 3 lines)
> 
> 
> 
> matju



Thanks to all who replied, especially matju for the explicit example!

Al


=====
Albert Davidson Chou

    Get answers to Mac questions at http://www.mac-mgrs.org/ .

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/