From: "Martin Boßlet" Date: 2012-03-17T02:26:34+09:00 Subject: Re: Converting Bignum into bytes > I'm trying to convert a variable size BigNum into a variable length byte > representation and I don't know if there exists a direct way. I'm thinking > of Array#pack and String#unpack. > > Something similar to, for ex: > big = 111111111111111111111111111111111111111 > bytes = [big].pack("l*") > and then > big = bytes.unpack("l*") > #pack and #unpack won't work for Bignums, but you can operate on their hex representations for example. The following should do it: big = 111111111111111111111111111111111111111 bytes = big.to_s(16).unpack('H*')[0] big2 = [bytes].pack('H*').to_i(16) puts big == big2 # => true Regards, Martin