From: jason@... (Jason Voegele) Date: 2001-10-05T23:00:50+09:00 Subject: [ruby-talk:22129] Re: Help with Array#pack Paul Brannan wrote in message news:... > On Fri, 5 Oct 2001, Jason Voegele wrote: > > > command = [1, 0, 1|2, 0x1000, 99, 0] > > > > These six entries in the array should correspond to the six fields in > > the message header above. So the above array should be converted to > > the binary string: > > > > BYTE 1 | BYTE 2 | BYTES 3&4 | BYTES 5-8 | BYTES 9-12 | BYTES 13-16 > > ------ | ------ | --------- | --------- | ---------- | ----------- > > 1 | 0 | 3 | 4096 | 99 | 0 > > Try this: > > def pack(arr) > arr.pack("CCnnnn") > end > > def display(x) > x.each_byte do |b| > puts b > end > end > > x = pack([1, 0, 1|2, 0x1000, 99, 0]) > display x > > The 'C' means unsigned char, and the 'n' means 16-bit short (in > network-byte-order, which is very important). Thanks for the information, Paul, and thanks for pointing out that it's big-endian order. I forgot to mention that. But doesn't pack("CCnnnn") lead to a 10 byte string rather than a 16 byte string? Note that field 3 is two bytes, while fields 4, 5, and 6 are four bytes. With this guidance, though, I think I've figured it out. The pack string should be "CCnNNN". That leads to a 16 byte string in the format I need. Again, thanks for your help. Jason Voegele