How to translate base 10 number into base 2 number

Hi all,

I want to write a method to tranlate base 10 number
into base 2 number. I want to call the method within
itself. But it doesn't work. What is the right way
to do something like this?

Thanks,

Li

···

#####
def ten_to_two(num1)
    @array=
     if num1>2
          first,second=num1.divmod(2)
          @array<<second
          
        #call the method itself
        ten_to_two(first)

     else
          @array<<second<<first
     end
    return @array.reverse
end
  
puts ten_to_two(5)

##output

ruby assembly1.rb

nil
nil

Exit code: 0

____________________________________________________________________________________
8:00? 8:25? 8:40? Find a flick in no time
with the Yahoo! Search movie showtime shortcut.
http://tools.search.yahoo.com/shortcuts/#news

Hi Li,

First, here's a solution:

def ten_to_two(num1)
  return if num1 <= 0
  first,second=num1.divmod(2)
  ten_to_two(first) << second
end

And note that you can change the << into a call to unshift if you want
the bits in the reverse order.

As for why your solution didn't work, let me offer two things. First,
you never do anything with the result of your recursive call to
ten_to_two. Somehow you need to combine it with the partial result you
calculated immediately beforehand. Second, you're using instance
variables (tagged with the @). That's generally done in methods
defined within a class to hold the state of the instance. For a
"naked" method, you generally stick to local variables (no @).

Eric

···

================
Interested in hands-on, on-site Ruby training? See www.LearnRuby.com
for information about a well-reviewed class.

chen li wrote:

Hi all,

I want to write a method to tranlate base 10 number
into base 2 number. I want to call the method within
itself. But it doesn't work. What is the right way
to do something like this?

Thanks,

Li

#####
def ten_to_two(num1)
    @array=
     if num1>2
          first,second=num1.divmod(2)
          @array<<second

        #call the method itself
        ten_to_two(first)

     else
          @array<<second<<first
     end
    return @array.reverse
end

puts ten_to_two(5)

##output
>ruby assembly1.rb
nil
nil
>Exit code: 0

This doesn't answer your question, but in case you were not aware, Ruby will do this conversion for you:

a = 125
puts a.to_s(2)
1111101

Kirk Haines

···

On Mon, 15 Jan 2007, chen li wrote:

Hi all,

I want to write a method to tranlate base 10 number
into base 2 number. I want to call the method within
itself. But it doesn't work. What is the right way
to do something like this?

This doesn't answer your question, but in case you
were not aware, Ruby
will do this conversion for you:

a = 125
puts a.to_s(2)
1111101

Kirk Haines

Thanks. But is is possible do the opposite, from
111101 to 125 change?

Li

···

____________________________________________________________________________________
8:00? 8:25? 8:40? Find a flick in no time
with the Yahoo! Search movie showtime shortcut.
http://tools.search.yahoo.com/shortcuts/#news

TAWLWTDIT

[0xFF].pack("c").unpack("b8")

there's a whole lotta ways to do it :slight_smile:

···

On 14/01/07, khaines@enigo.com <khaines@enigo.com> wrote:

On Mon, 15 Jan 2007, chen li wrote:

> Hi all,
>
> I want to write a method to tranlate base 10 number
> into base 2 number. I want to call the method within
> itself. But it doesn't work. What is the right way
> to do something like this?

This doesn't answer your question, but in case you were not aware, Ruby
will do this conversion for you:

a = 125
puts a.to_s(2)
1111101

Kirk Haines

--
best,
                        UG
---
Uma Geller

Is there a way to pad the result with leading zeros
i.e. 4.to_s(2)
"100"
What I am trying to do is sort ip addresses
i.e. "192.61.14.30"
When I try to convert "192.61.14.30"
a ="192.61.14.30".split(/\./)
b = a.collect{|i| i.to_i.to_s(2) }
p b
["11000000","111101", "1110", "11110"]
p b.join
"11000000111101111011110".to_i

How would you pad the results of .to_s(2) to come up with a byte?

Thank you,
raymond

···

-----Original Message-----
From: khaines@enigo.com [mailto:khaines@enigo.com]
Sent: Sunday, January 14, 2007 12:07
To: ruby-talk ML
Subject: Re: how to translate base 10 number into base 2 number

On Mon, 15 Jan 2007, chen li wrote:

Hi all,

I want to write a method to tranlate base 10 number into base 2
number. I want to call the method within itself. But it doesn't work.
What is the right way to do something like this?

This doesn't answer your question, but in case you were not aware, Ruby
will do this conversion for you:

a = 125
puts a.to_s(2)
1111101

Kirk Haines

Hi Eric,

Thanks for the explanations. It helps me out.

Li

···

--- "Eric I." <rubytraining@gmail.com> wrote:

Hi Li,

First, here's a solution:

def ten_to_two(num1)
  return if num1 <= 0
  first,second=num1.divmod(2)
  ten_to_two(first) << second
end

And note that you can change the << into a call to
unshift if you want
the bits in the reverse order.

As for why your solution didn't work, let me offer
two things. First,
you never do anything with the result of your
recursive call to
ten_to_two. Somehow you need to combine it with the
partial result you
calculated immediately beforehand. Second, you're
using instance
variables (tagged with the @). That's generally
done in methods
defined within a class to hold the state of the
instance. For a
"naked" method, you generally stick to local
variables (no @).

Eric

____________________________________________________________________________________
Get your own web address.
Have a HUGE year through Yahoo! Small Business.

"111101".to_i(2)
=> 125

···

On 14-Jan-07, at 12:19 PM, chen li wrote:

This doesn't answer your question, but in case you
were not aware, Ruby
will do this conversion for you:

a = 125
puts a.to_s(2)
1111101

Kirk Haines

Thanks. But is is possible do the opposite, from
111101 to 125 change?

--
Jeremy Tregunna
jtregunna@blurgle.ca

puts '1111101'.to_i(2)
125

Kirk Haines

···

On Mon, 15 Jan 2007, chen li wrote:

a = 125
puts a.to_s(2)
1111101

Kirk Haines

Thanks. But is is possible do the opposite, from
111101 to 125 change?

Since we start collecting solutions...

irb(main):001:0> "%b" % 16
=> "10000"

:slight_smile:

  robert

···

On 14.01.2007 19:25, Uma Geller wrote:

TAWLWTDIT

[0xFF].pack("c").unpack("b8")

there's a whole lotta ways to do it :slight_smile:

irb(main):004:0> "%08b" % 4
=> "00000100"
irb(main):005:0> sprintf "%08b", 4
=> "00000100"

  robert

···

On 15.01.2007 12:54, Jacob, Raymond A Jr wrote:

Is there a way to pad the result with leading zeros
i.e. 4.to_s(2)
"100"

Jacob, Raymond A Jr wrote:
(speaking of strings...)

Is there a way to pad the result with leading zeros

$ ri String#rjust

----------------------------------------------------------- String#rjust
      str.rjust(integer, padstr=' ') => new_str

···

------------------------------------------------------------------------
      If _integer_ is greater than the length of _str_, returns a new
      +String+ of length _integer_ with _str_ right justified and padded
      with _padstr_; otherwise, returns _str_.

         "hello".rjust(4) #=> "hello"
         "hello".rjust(20) #=> " hello"
         "hello".rjust(20, '1234') #=> "123412341234123hello"

HTH
--

I wonder why there isn't support for negative radix; even though the
solutions might not be unique... who cares?

···

On 1/17/07, chen li <chen_li3@yahoo.com> wrote:

--- "Eric I." <rubytraining@gmail.com> wrote:

> Hi Li,
>
> First, here's a solution:
>
> def ten_to_two(num1)
> return if num1 <= 0
> first,second=num1.divmod(2)
> ten_to_two(first) << second
> end
>
> And note that you can change the << into a call to
> unshift if you want
> the bits in the reverse order.
>
> As for why your solution didn't work, let me offer
> two things. First,
> you never do anything with the result of your
> recursive call to
> ten_to_two. Somehow you need to combine it with the
> partial result you
> calculated immediately beforehand. Second, you're
> using instance
> variables (tagged with the @). That's generally
> done in methods
> defined within a class to hold the state of the
> instance. For a
> "naked" method, you generally stick to local
> variables (no @).
>
> Eric

Hi Eric,

Thanks for the explanations. It helps me out.

Li

____________________________________________________________________________________
Get your own web address.
Have a HUGE year through Yahoo! Small Business.
Buy Website Domain Name for Business - Buy Online Domain - Turbify

Since we start collecting solutions...

irb(main):001:0> "%b" % 16
=> "10000"

readable and concise
beautiful !

                        UG

···

---
Uma Geller

Thank you,
Raymond

···

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: Monday, January 15, 2007 7:00
To: ruby-talk ML
Subject: Re: how to translate base 10 number into base 2 number

On 15.01.2007 12:54, Jacob, Raymond A Jr wrote:

Is there a way to pad the result with leading zeros i.e. 4.to_s(2)
"100"

irb(main):004:0> "%08b" % 4
=> "00000100"
irb(main):005:0> sprintf "%08b", 4
=> "00000100"

  robert

Come on, that's like asking why fractional dimension isn't a primitive
type in Java.

···

On 1/23/07, Roland Mai <roland.mai@gmail.com> wrote:

I wonder why there isn't support for negative radix; even though the
solutions might not be unique... who cares?

--
Giles Bowkett
http://www.gilesgoatboy.org

http://gilesgoatboy.blogspot.com