Forum: Ruby how to tranlsate number to binary?

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.
6d59f83e787fddec1b6f4aebb86b1613?d=identicon&s=25 Ak 756 (ak756)
on 2007-03-26 03:26
Hi, I am a ruby newbie. I want to write a program which will scan a
input file, change every digital number into  4 bits len's binary
number.
For example, for input.txt as

903
1047

I will get a new file as

100100000011
0001000001000111

Would anyboby kindly help to tell me how to do this?
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2007-03-26 03:38
(Received via mailing list)
On 3/26/07, Ak 756 <macro.peng@gmail.com> wrote:
> 100100000011
> 0001000001000111
>
> Would anyboby kindly help to tell me how to do this?

I don't know what a "4 bits len's binary number" is.

But here's how you convert decimal to binary in Ruby:

irb(main):004:0> 903.to_s(2)
=> "1110000111"
irb(main):005:0> 1+2+4+128+256+512
=> 903

Hope that helps,
-Harold
D91500918a07bfae5b38b8943fd42fbe?d=identicon&s=25 Mike Moore (Guest)
on 2007-03-26 04:18
(Received via mailing list)
I assume that by "4 bits len binary number" he means that he wants the
binary number to be a multiple of 4 characters long.  So the binary
number
would have "0" prepended to it if the size wasn't divisible by 4.  Here
is
an overly verbose way to convert the num to the binary number string.

irb(main):001:0> def int_to_binary(num)
irb(main):002:1>   binary_num = num.to_s(2)
irb(main):003:1>   if ((binary_num.size % 4) > 0)
irb(main):004:2>     binary_num = ('0' * (4 - (binary_num.size % 4))) +
binary_num
irb(main):005:2>   end
irb(main):006:1>   binary_num
irb(main):007:1> end
=> nil
irb(main):008:0> int_to_binary(903)
=> "001110000111"
irb(main):009:0> int_to_binary(15)
=> "1111"
irb(main):010:0> int_to_binary(16)
=> "00010000"
irb(main):011:0>
6d59f83e787fddec1b6f4aebb86b1613?d=identicon&s=25 Ak 756 (ak756)
on 2007-03-26 05:32
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?
807270f56f26ad90755eef71f2c228fe?d=identicon&s=25 Alex Gutteridge (Guest)
on 2007-03-26 06:04
(Received via mailing list)
On 26 Mar 2007, at 12:32, Ak 756 wrote:

> --
> Posted via http://www.ruby-forum.com/.
>

903.to_s.split(//).map{|n| sprintf("%04d",n.to_i.to_s(2))}.join

Alex Gutteridge

Bioinformatics Center
Kyoto University
637f745e44c81f4830ba7dca35c800b4?d=identicon&s=25 Mike (Guest)
on 2007-03-26 06:05
(Received via mailing list)
On Mar 26, 1:32 pm, Ak 756 <macro.p...@gmail.com> wrote:
> Posted viahttp://www.ruby-forum.com/.
Maybe this one is goon for you?

903.to_s.split(//).map{|x| tmp=x.to_i.to_s(2); "0"*(4-tmp.length)
+tmp}.join
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-03-26 06:07
(Received via mailing list)
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?

"903".split("").map{|s| "%04b" % s.to_i}.join
=> "100100000011"
7f891fbe8e3bae7f9fe375407ce90d9d?d=identicon&s=25 Harold Hausman (Guest)
on 2007-03-26 06:07
(Received via mailing list)
On 3/26/07, Ak 756 <macro.peng@gmail.com> 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?
>

I don't know about efficent, but you could start with something like
this and see if it's performant enough:

irb(main):002:0> 903.to_s.split("").each {|digit| p digit.to_i.to_s(2)}
"1001"
"0"
"11"

Meh, actually, I bet someone else can do better than that, but I'm at
work right now. (:

-Harold
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2007-03-26 06:11
(Received via mailing list)
On 3/25/07, Ak 756 <macro.peng@gmail.com> wrote:
> --
> 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
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-03-26 06:55
(Received via mailing list)
On Mar 25, 10:06 pm, Joel VanderWerf <v...@path.berkeley.edu> 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"
6d59f83e787fddec1b6f4aebb86b1613?d=identicon&s=25 Ak 756 (ak756)
on 2007-03-26 07:00
Gavin Kistner wrote:
> On Mar 25, 10:06 pm, Joel VanderWerf <v...@path.berkeley.edu> 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"

Woo.., that's really what's I want.
Thanks you guys-:)
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-03-26 07:04
(Received via mailing list)
Phrogz wrote:
> 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"
19fdf8bd123216b5056fb856cf1a5771?d=identicon&s=25 _why (Guest)
on 2007-03-26 08:54
(Received via mailing list)
On Mon, Mar 26, 2007 at 10:26:35AM +0900, Ak 756 wrote:
> For example, for input.txt as
>
> 903
> 1047
>
> I will get a new file as
>
> 100100000011
> 0001000001000111
>

  >> 0x903.to_s(2)
  => "100100000011"
  >> 0x1047.to_s(2)
  => "1000001000111"

And so it was:

  puts IO.read('input.txt').map { |x| x.to_i(16).to_s(2) }.join

_why
F1ae87d23c050dbad815489dbde2f226?d=identicon&s=25 G.Durga Prasad (Guest)
on 2007-03-26 14:17
(Received via mailing list)
On 3/26/07, _why <why@ruby-lang.org> wrote:
> >
> _why
>
>

>> format("%0#{"193".length*4}b", 0x193)
=> "000110010011"
     to get leading zeros.

And so , the following
puts IO.read('input.txt').map { |x|
format("%0#{x.chomp.length*4}b",x.to_i(16)) }.join("\n")

Prasad
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-27 17:59
(Received via mailing list)
On 3/25/07, Ak 756 <macro.peng@gmail.com> 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?

Just for the record, that is commonly called BCD (for Binary Coded
Decimal).

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
This topic is locked and can not be replied to.