Simple Q: bit string -> integer

Is there a simple way to induce a bit string into an
integer; for example, “10010000” into 144? And also,
to show a Fixnum in its different representations
(hex, num, binary)?

On 5/6/07, Todd B. [email protected] wrote:

Try this

str = “10010000”
p str.to_i(2)
puts
p 255.to_s(16)
p 255.to_s(2)

Harry

— Harry K. [email protected] wrote:

Try this

str = “10010000”
p str.to_i(2)
puts
p 255.to_s(16)
p 255.to_s(2)

Harry

Yes, thanks a bunch. For some reason, #to_i was the
last place I thought to look.

Todd

Todd B. wrote:

Is there a simple way to induce a bit string into an
integer; for example, “10010000” into 144?

“10010000”.to_i(2)

And also,
to show a Fixnum in its different representations
(hex, num, binary)?

144.to_s(16), 144.to_s(2)

But what’s “num”?

— Florian F. [email protected] wrote:

Florian F.
“num” as in my brain is “numb” from staring at 1’s and 0’s.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs