std method to convert string of bytes into Integer?

S

Sam Roberts

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
 
C

Carlos

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.
 
C

Carlos

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 :))).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top