[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

std method to convert string of bytes into Integer?

Sam Roberts

11/26/2004 4:19:00 PM

I've a string of bytes, represented as a String, most significant byte
first, and I want to convert it to an Integer (actually, they will
usually be big enough that they will end up as a Bignum).

I thought String#unpack, but it won't deal with anything larger than 4
or 8 bytes, it seems.

I don't think String#to_i will do it either.

I've written such a method myself, but I'd like to not duplicate the std
lib is possible!

Cheers,
Sam



2 Answers

Carlos

11/26/2004 6:01:00 PM

0

[Sam Roberts <sroberts@uniserve.com>, 2004-11-26 17.19 CET]
> I've a string of bytes, represented as a String, most significant byte
> first, and I want to convert it to an Integer (actually, they will
> usually be big enough that they will end up as a Bignum).
>
> I thought String#unpack, but it won't deal with anything larger than 4
> or 8 bytes, it seems.
>
> I don't think String#to_i will do it either.
>
> I've written such a method myself, but I'd like to not duplicate the std
> lib is possible!

I don't know if this is correct:

"01234567890123456789".unpack("H20")
=> ["30313233343536373839"]

Good luck.


Carlos

11/26/2004 6:04:00 PM

0

[Carlos <angus@quovadis.com.ar>, 2004-11-26 19.01 CET]
> I don't know if this is correct:
>
> "01234567890123456789".unpack("H20")
> => ["30313233343536373839"]

Oops... it isn't. It is a string. I was trying randomly until I saw a big
number :))).