From: Robert Feldt Date: 2003-05-29T20:37:07+09:00 Subject: Re: RCR: unpack/pack Bignum On Thu, 29 May 2003 nobu.nokada@softhome.net wrote: > I guess "N(2)" is equivalent to "n", so the behavior should be > consist with the current one. But it differs for Fixnum and > Bignum. Which is better? > > $ ruby -e 'p [0x010203].pack("n")' > "\002\003" > > $ ruby -e 'p [0x0102030405].pack("n")' > -e:1:in `pack': bignum too big to convert into `int' (RangeError) > from -e:1 > The former seems better to me. So we truncate MSB bytes until it fits. > > How can this be used when I have a Bignum that I don't know how large it > > will be? 'N(*)'? > > I expect '(*)' would mean to convert all remained data like as > 'a*' does, though you need to know the exact template to pack/ > unpack fundamentally. > Yes, but this should work then s = [large_bignum_or_really_any_int].pack("N(*)") num = s.unpack("N(#{s.length})").first # now num == large_bitnum_or_really_any_int ? If we can agree on something along those lines I think it would be great. Very useful, imho. Any opinion from matz or others? /Robert