------art_266308_5443828.1174875453454
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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  um.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>

On 3/25/07, Harold Hausman <hhausman / gmail.com> wrote:
>
> On 3/26/07, Ak 756 <macro.peng / gmail.com> wrote:
> > 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?
>
> 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
>
>

------art_266308_5443828.1174875453454--