From: Mike Stok Date: 2002-10-09T02:10:30+09:00 Subject: Re: Problem with pack and Hh format specifiers In article <1034037350.867.30.camel@awaji>, Tony Sloane wrote: > Hi, > > I'm relatively new to Ruby so perhaps I'm missing something, but I can't > get Array's pack to work in the following situation: > > irb(main):001:0> "%08b" % ["4","7"].pack ('HXh')[0] > "00000111" > > I'm expecting to see "01000111". What are you really trying to do? There are some ugly ways to get ['4', '7'] into a string of 1's and 0s: irb(main):001:0> sprintf('%08b', Integer("0x" + ['4', '7'].join(''))) "01000111" irb(main):002:0> '%08b' % [["4", "7"].join("")].pack('H*')[0] "01000111" irb(main):003:0> ['47'].pack('H*').unpack('B*') ["01000111"] > Note that unpack seems to work the way I would expect. (ASCII code for > 'G' is 01000111.) > > irb(main):002:0> "G".unpack ('HXh') > ["4", "7"] Depending on your context you may want to use H* as a template and split the resulting string e.g. irb(main):005:0> "G".unpack('H*') ["47"] irb(main):006:0> 'banana'.unpack('H*') ["62616e616e61"] I hope that I'm not off on some wild tangent, and that this is of some use. Regards, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA