From: Paul Battley Date: 2006-06-18T04:58:29+09:00 Subject: Re: Generate a hex string from integers On 17/06/06, Xavier Noria wrote: > On Jun 17, 2006, at 20:52, Michael stepanov wrote: > > > I'm new in Ruby. So, my question can seem simple for you. I need to > > generate a hex string from some digits: 1, 114, 3, 2 for example. I > > need > > the following hex representation: 00010072030002 - (0x0001, 0x0072, > > 0x03, 0x0002). > > sprintf to the rescue! > > irb(main):002:0> sprintf "%04x"*4, 1, 114, 3, 2 > => "0001007200030002" Not a bad idea, but that won't exactly do it: he wants only one octet for the 3. Try this: hex_str = a.pack('nnCn').unpack('H*')[0] # => "00010072030002" Paul.