From: Patrick Hurley Date: 2007-03-26T13:10:59+09:00 Subject: Re: how to tranlsate number to binary? On 3/25/07, Ak 756 wrote: > Mike Moore wrote: > > > irb(main):008:0> int_to_binary(903) > > => "001110000111" > > Hi ,thanks for advise. > 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? > > -- > Posted via http://www.ruby-forum.com/. > > irb(main):001:0> "903".split(//).map { |c| "%04b" % c } => ["1001", "0000", "0011"] irb(main):002:0> "903".split(//).map { |c| "%04b" % c }.join => "100100000011" irb(main):003:0> "903".split(//).map { |c| "%04b" % c }.join(' ') => "1001 0000 0011" irb(main):004:0> Something like that I would work,another approach would be to use pack with an H to then process the resulting string in bits. pth