From: Joel VanderWerf Date: 2007-03-26T14:03:45+09:00 Subject: Re: how to tranlsate number to binary? Phrogz wrote: > On Mar 25, 10:06 pm, Joel VanderWerf wrote: >> Ak 756 wrote: >>> I want my function to give output 100100000011 for input 903. Where >>> '1001','0000','0011' is the binary value for 9,0,3 >>> Is there efficient way to do this? >> "903".split("").map{|s| "%04b" % s.to_i}.join >> => "100100000011" > > No need to call to_i on the digit strings: > > irb(main):002:0> 903.to_s.split('').map{|n| "%04b" % n }.to_s > => "100100000011" Had no idea that would work, but now it seems obvious.... Apparently #Integer is being called by #% when the format char is 'b', because it works correctly with different bases: "%04b" % "16" => "10000" "%04b" % "0x10" => "10000" "%04b" % "0b10000" => "10000" -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407