8 bit binary conversion

Can anyone help me to convert an integer to 8-bit binary from ruby.

like-- 4-00000100

Thanks,
Dhanabal

On Mon, Oct 14, 2013 at 5:39 PM, Dhanabal Thangavel
[email protected] 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

even better than my routine! Again I bow down to Robert! :slight_smile:

----- Original Message -----
From: Robert K. [email protected]
To: Ruby users [email protected]
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
[email protected] 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

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

Thank u all, i got one more

6.to_s(2)
=> “110”
6.to_s(2).rjust(8,‘0’)
=> “00000110”

Thanks,
Dhanabal

On Oct 14, 2013, at 11:04 AM, Dhanabal [email protected] wrote:

Thank u all, i got one more

6.to_s(2)
=> “110”
6.to_s(2).rjust(8,‘0’)
=> “00000110”

Have not seen that one before!

On Oct 14, 2013, at 10:54 AM, Robert K. [email protected]
wrote:

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

On Tue, Oct 15, 2013 at 12:39 AM, Dhanabal Thangavel <
[email protected]> wrote:

Can anyone help me to convert an integer to 8-bit binary from ruby.

like-- 4-00000100

Thanks,
Dhanabal

What? Nobody did it this way?

The way I would do it was already mentioned.
So, here is a way that nobody said yet. :slight_smile:
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 6:56 AM, Harry K. [email protected]
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. :slight_smile:
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”

?

I forgot to mention one approach: use String#%

irb(main):001:0> “%08b” % 4
=> “00000100”

Cheers

robert

On Tue, Oct 15, 2013 at 3:36 PM, Robert K.
[email protected]wrote:

a = 5

That’s cool.
It would be even cooler if it worked. :slight_smile:
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 K.
[email protected]wrote:

a = 5

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 9:52 AM, Tamara T.
[email protected]wrote:

irb(main):001:0> 4.to_s(2)

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.
I didn’t notice it before because I didn’t know what I was looking at.
:slight_smile:
I learned something from this thread.

Harry

On Tue, Oct 15, 2013 at 3:53 PM, Harry K. [email protected]
wrote:

I am not suggesting that you use it.
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.

Ugh, sorry for that. Yes, this is quite embarrassingly not a binary
number.

:slight_smile:

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 2:52 AM, Tamara T. [email protected]
wrote:

=> “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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs