From: Mathieu Bouchard Date: 2001-07-10T08:19:34+09:00 Subject: [ruby-talk:17565] Re: writing arbitrary data to a socket 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