Todd Benson wrote: > On 11/6/07, yermej <yermej / gmail.com> wrote: >> On Nov 6, 6:42 pm, Collin VanDyck <gluedtomys... / gmail.com> wrote: >>> Hello -- I'm trying to write out specific byte sequences over the >>> wire / to a file / etc. Let's say for example that I wanted to write >>> out >>> >>> 99 111 108 108 105 110 >>> >>> as a simple, six byte sequence to a file. In Java, I might use the >>> byte primitive, but it seems that in Ruby you have Fixnum and then a >>> Float for fractions. If I simply create a Fixnum using the 99 >>> literal, I think that that will create more than an 8-bit sequence >>> when I write out to the file. >>> >>> Any ideas on how to do this? >> I *think* you'll want to use Array#pack. E.g., to write them out in >> big-endian order: >> >>> a = [99, 111, 108, 108, 105, 110].pack 'n*' >> => "\000c\000o\000l\000l\000i\000n" >>> file.write a >> If you want little-endian, use 's*' (or 'S*' for unsigned) instead of >> 'n*'. See http://ruby-doc.org/core/ and check Array#pack for more >> details on the directives. > > Hmm, I was thinking about #pack, but your example creates 12 bytes. > That's because n isn't the code for a byte - you want c or C, for signed or unsigned char: irb(main):004:0> a = [1, 2, 3, 4, 5, 6].pack 'C*' => "\001\002\003\004\005\006" -- Alex