Forum: Ruby Converting Integer to binary string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2006-02-22 07:05
(Received via mailing list)
Hello all,

I tried searching the list for this, but didn't see the answer
anywhere. A lot of people trying to do the opposite, but.. :)

I'd like to convert the Integer 585 to the string "1001001001"

I see from the docs for Fixnum#[] that I could hack something together:

30.downto(0) { |i| print 585[i] }

But sometimes you just *know* there's a better way...

Thanks in advance,
-Harold
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-22 07:08
(Received via mailing list)
585.to_s(2)

--
Kent
---
http://www.datanoise.com
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2006-02-22 07:26
(Received via mailing list)
Dah! Thank you.

Man, I was trying all kinds of crazy business.

585.to_s.to_i(2)

:(

Thanks again :P,
-Harold
This topic is locked and can not be replied to.