[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to tranlsate number to binary?

Ak 756

3/26/2007 1:27:00 AM

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?

--
Posted via http://www.ruby-....

13 Answers

Harold Hausman

3/26/2007 1:38:00 AM

0

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

Ak 756

3/26/2007 3:33:00 AM

0

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

Mike

3/26/2007 4:02:00 AM

0

On Mar 26, 1:32 pm, Ak 756 <macro.p...@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?
>
> --
> Posted viahttp://www.ruby-....

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

Alex Gutteridge

3/26/2007 4:04:00 AM

0

On 26 Mar 2007, at 12:32, 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-....
>

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

Alex Gutteridge

Bioinformatics Center
Kyoto University



Joel VanderWerf

3/26/2007 4:06:00 AM

0

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"



--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Harold Hausman

3/26/2007 4:06:00 AM

0

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

Patrick Hurley

3/26/2007 4:11:00 AM

0

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?
>
> --
> Posted via http://www.ruby-....
>
>

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

Gavin Kistner

3/26/2007 4:53:00 AM

0

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"

Ak 756

3/26/2007 5:00:00 AM

0

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-:)

--
Posted via http://www.ruby-....

Joel VanderWerf

3/26/2007 5:04:00 AM

0

Phrogz 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"

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