Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
Thanks,
Dhanabal
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
Thanks,
Dhanabal
Here is some code I wrote to do various conversions:
class String
def convert_base(from, to)
self.to_i(from).to_s(to)
end
end
Then when I need to use it:
irb(main):009:0> 4.to_s.convert_base(10, 2)
=> "100"
If you want leading zeros, you'll have to come up with a simple routine for that.
Wayne
even better than my routine! Again I bow down to Robert!
----- Original Message -----
From: Robert Klemme <shortcutter@googlemail.com>
To: Ruby users <ruby-talk@ruby-lang.org>
Cc:
Sent: Monday, October 14, 2013 10:54 AM
Subject: Re: 8 bit binary conversion
On Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel <ruby.dhanabal@gmail.com> wrote:
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
irb(main):001:0> 4.to_s(2)
=> "100"
irb(main):002:0> sprintf("%08b", 4)
=> "00000100"
Cheers
robert
irb(main):001:0> 4.to_s(2)
=> "100"
irb(main):002:0> sprintf("%08b", 4)
=> "00000100"
Cheers
robert
On Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel <ruby.dhanabal@gmail.com> wrote:
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Hi, Robert! o/
Is there any reason to prefer sprintf over the % operator here? As an old-time C hacker, sprintf rolls off the fingers, but
"%08b" % 4
looks a weee dram more rubyish. Thoughts?
Tamara
On Oct 14, 2013, at 10:54 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel > <ruby.dhanabal@gmail.com> wrote:
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
irb(main):001:0> 4.to_s(2)
=> "100"
irb(main):002:0> sprintf("%08b", 4)
=> "00000100"Cheers
robert
Thank u all, i got one more
6.to_s(2)
=> "110"
6.to_s(2).rjust(8,'0')
=> "00000110"
Thanks,
Dhanabal
On Monday 14 October 2013 09:27 PM, Wayne Brisette wrote:
even better than my routine! Again I bow down to Robert!
----- Original Message -----
From: Robert Klemme <shortcutter@googlemail.com>
To: Ruby users <ruby-talk@ruby-lang.org>
Cc:
Sent: Monday, October 14, 2013 10:54 AM
Subject: Re: 8 bit binary conversionOn Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel > <ruby.dhanabal@gmail.com> wrote:
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
irb(main):001:0> 4.to_s(2)
=> "100"
irb(main):002:0> sprintf("%08b", 4)
=> "00000100"Cheers
robert
What? Nobody did it this way?
The way I would do it was already mentioned.
So, here is a way that nobody said yet.
I am not suggesting that you use it.
I was just messing around.
a = 5
p "".tap{|s| 8.times{|x| s.insert(0,((a/2**x)%2).to_s)}}
Harry
On Tue, Oct 15, 2013 at 12:39 AM, Dhanabal Thangavel < ruby.dhanabal@gmail.com> wrote:
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
Thanks,
Dhanabal
Sorry for the late reply, somehow GMail's logic of marking things as
"read" which are not actually read made your reply disappear from my
radar.
To answer your question: I use String#% only if there is just one
argument. Otherwise you have to use an Array for the arguments which
IMHO makes it much more cumbersome than (s)printf:
irb(main):001:0> "%s = %6.2f" % ["foo", 123.456]
=> "foo = 123.46"
irb(main):002:0> sprintf "%s = %6.2f", "foo", 123.456
=> "foo = 123.46"
Kind regards
robert
On Tue, Oct 15, 2013 at 2:52 AM, Tamara Temple <tamouse.lists@gmail.com> wrote:
On Oct 14, 2013, at 10:54 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel >> <ruby.dhanabal@gmail.com> wrote:
Can anyone help me to convert an integer to 8-bit binary from ruby.
like-- 4-00000100
irb(main):001:0> 4.to_s(2)
=> "100"
irb(main):002:0> sprintf("%08b", 4)
=> "00000100"
Is there any reason to prefer sprintf over the % operator here? As an old-time C hacker, sprintf rolls off the fingers, but
"%08b" % 4
looks a weee dram more rubyish. Thoughts?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
I didn't notice it before because I didn't know what I was looking at.
I learned something from this thread.
Harry
On Tue, Oct 15, 2013 at 9:52 AM, Tamara Temple <tamouse.lists@gmail.com>wrote:
On Oct 14, 2013, at 10:54 AM, Robert Klemme <shortcutter@googlemail.com> > wrote:
> On Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel > > <ruby.dhanabal@gmail.com> wrote:
>> Can anyone help me to convert an integer to 8-bit binary from ruby.
>>
>> like-- 4-00000100
>
> irb(main):001:0> 4.to_s(2)
> => "100"
> irb(main):002:0> sprintf("%08b", 4)
> => "00000100"
>
> Cheers
>
> robert
>Hi, Robert! o/
Is there any reason to prefer sprintf over the % operator here? As an
old-time C hacker, sprintf rolls off the fingers, but"%08b" % 4
looks a weee dram more rubyish. Thoughts?
Tamara
Thank you for posting that approach.
Have not seen that one before!
On Oct 14, 2013, at 11:04 AM, Dhanabal <ruby.dhanabal@gmail.com> wrote:
Thank u all, i got one more
6.to_s(2)
=> "110"
6.to_s(2).rjust(8,'0')
=> "00000110"
:-))
How about
irb(main):004:0> 8.times.map {|i| 4 & 1 << i}.reverse.join
=> "00000400"
?
I forgot to mention one approach: use String#%
irb(main):001:0> "%08b" % 4
=> "00000100"
Cheers
robert
On Tue, Oct 15, 2013 at 6:56 AM, Harry Kakueki <list.push@gmail.com> wrote:
What? Nobody did it this way?
The way I would do it was already mentioned.
So, here is a way that nobody said yet.
I am not suggesting that you use it.
I was just messing around.a = 5
p "".tap{|s| 8.times{|x| s.insert(0,((a/2**x)%2).to_s)}}
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
> What? Nobody did it this way?
>
> The way I would do it was already mentioned.
> So, here is a way that nobody said yet.
> I am not suggesting that you use it.
> I was just messing around.
>
> a = 5
>
> p "".tap{|s| 8.times{|x| s.insert(0,((a/2**x)%2).to_s)}}:-))
How about
irb(main):004:0> 8.times.map {|i| 4 & 1 << i}.reverse.join
=> "00000400"
That's cool.
It would be even cooler if it worked.
Or is that what you wanted to do?
I forgot to mention one approach: use String#%
irb(main):001:0> "%08b" % 4
I learned something here. Thanks.
Harry
On Tue, Oct 15, 2013 at 3:36 PM, Robert Klemme <shortcutter@googlemail.com>wrote:
On Tue, Oct 15, 2013 at 6:56 AM, Harry Kakueki <list.push@gmail.com> > wrote:
Seriously, I did like this idea when I saw it, but the result is not a
binary number.
I want to try something like it but I have not had time yet.
Harry
On Tue, Oct 15, 2013 at 3:36 PM, Robert Klemme <shortcutter@googlemail.com>wrote:
On Tue, Oct 15, 2013 at 6:56 AM, Harry Kakueki <list.push@gmail.com> > wrote:
> What? Nobody did it this way?
>
> The way I would do it was already mentioned.
> So, here is a way that nobody said yet.
> I am not suggesting that you use it.
> I was just messing around.
>
> a = 5
>
> p "".tap{|s| 8.times{|x| s.insert(0,((a/2**x)%2).to_s)}}:-))
How about
irb(main):004:0> 8.times.map {|i| 4 & 1 << i}.reverse.join
=> "00000400"
Ugh, sorry for that. Yes, this is quite embarrassingly not a binary number.
irb(main):002:0> 8.times.map {|i| (4 & 1 << i) >> i}.reverse.join
=> "00000100"
How about that?
Cheers
robert
On Tue, Oct 15, 2013 at 3:53 PM, Harry Kakueki <list.push@gmail.com> wrote:
On Tue, Oct 15, 2013 at 3:36 PM, Robert Klemme <shortcutter@googlemail.com> > wrote:
On Tue, Oct 15, 2013 at 6:56 AM, Harry Kakueki <list.push@gmail.com> >> wrote:
> What? Nobody did it this way?
>
> The way I would do it was already mentioned.
> So, here is a way that nobody said yet.
> I am not suggesting that you use it.
> I was just messing around.
>
> a = 5
>
> p "".tap{|s| 8.times{|x| s.insert(0,((a/2**x)%2).to_s)}}:-))
How about
irb(main):004:0> 8.times.map {|i| 4 & 1 << i}.reverse.join
=> "00000400"Seriously, I did like this idea when I saw it, but the result is not a
binary number.
I want to try something like it but I have not had time yet.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/