From: Victor Blaga Date: 2012-03-17T02:50:26+09:00 Subject: Re: Converting Bignum into bytes --f46d040710ab6133da04bb5fd758 Content-Type: text/plain; charset=ISO-8859-1 Hi Martin, Thank you for your answer. I also thought of this solution, but a little bit the other way around, like this: big = 11111111111111111111111111111111111111 bytes = [big.to_s(16)].pack("H*") => "\x85\xBE\xC1+K\x9A@\xD8\xF4\x83\xCB\x1Cq\xC7\x1Cp" big2 = bytes.unpack("H*")[0].to_i(16) However... puts big2==big #=>false Taking a closer look... bytes.unpack("H*")[0] => "85bec12b4b9a40d8f483cb1c71c71c70" compared to big.to_s(16) => "85bec12b4b9a40d8f483cb1c71c71c7" The base 16 length representation of big is 31 whereas the unpacked string is 32 bytes. I wonder is this a bug? In any case I guess the solution is to pad the big.to_s(16) String with 0 on the left side until reaching a round (as in base 2 round) number :) Kind regards, Victor --f46d040710ab6133da04bb5fd758 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi Martin,

Thank you for your answer. I also thought of = this solution, but a little bit the other way around, like this:
=

big =3D 11111111111111111111111111111111111111
bytes =3D [big.to_s(16)].pack("H*")
=A0=3D> "\x= 85\xBE\xC1+K\x9A@\xD8\xF4\x83\xCB\x1Cq\xC7\x1Cp"
big2 = =3D bytes.unpack("H*")[0].to_i(16)

H= owever...
puts big2=3D=3Dbig #=3D>false

Taking a clo= ser look...
bytes.unpack("H*")[0]
=A0=3D> = "85bec12b4b9a40d8f483cb1c71c71c70"
compared to
big.to_s(16)
=A0=3D> "85bec12b4b9a40d8f483cb1c71c71c7"
<= br>
The base 16 length representation of big is 31 whereas the un= packed string is 32 bytes.
I wonder is this a bug?

In any case I guess the solution is to pad the big.to_s(16) Stri= ng with 0 on the left side until reaching a round (as in base 2 round) numb= er :)

Kind regards,
Victor
--f46d040710ab6133da04bb5fd758--