From: Robert Klemme Date: 2009-07-01T00:26:12+09:00 Subject: Re: Question on networking with custom binary interface. 2009/6/30 Greg Chambers : > Joel VanderWerf wrote: >> Greg Chambers wrote: >>> Any advice for doing this type of binary IO? >> >> Array#pack, String#unpack are your friends. > > I am looking at the documentation of these methods and this covers a LOT > of what I need to do, but I am worried in a couple areas.  For example, > part of the binary string I will be getting will contain an eight byte > unsigned long long.  How should I approach custom byte structures like > this? irb(main):004:0> s = (1..8).map {|x| x.chr}.join => "\x01\x02\x03\x04\x05\x06\a\b" irb(main):005:0> x = s.unpack "Q" => [578437695752307201] irb(main):006:0> x.first.class => Bignum If you need more flexibility you can do something like this irb(main):009:0> s.unpack("N2").inject(0) {|s,a| s << 32 | a} => 72623859790382856 irb(main):010:0> s.unpack("L2").inject(0) {|s,a| s << 32 | a} => 289077004534744581 Note, I did not bother to check byte orderings which you can easily guess from the different results. This is just an illustration to give you an idea. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/