How to tranlsate number to binary?


#1

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?


#2

On 3/26/07, Ak 756 removed_email_address@domain.invalid wrote:

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


#3

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


#4

Mike M. 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?


#5

On 26 Mar 2007, at 12:32, Ak 756 wrote:


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

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

Alex G.

Bioinformatics Center
Kyoto University


#6

On Mar 26, 1:32 pm, Ak 756 removed_email_address@domain.invalid wrote:

Posted viahttp://www.ruby-forum.com/.
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


#7

On 3/26/07, Ak 756 removed_email_address@domain.invalid wrote:

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


#8

Ak 756 wrote:

Mike M. 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”


#9

On 3/25/07, Ak 756 removed_email_address@domain.invalid wrote:


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

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


#10

Gavin K. wrote:

On Mar 25, 10:06 pm, Joel VanderWerf removed_email_address@domain.invalid 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-:slight_smile:


#11

On Mar 25, 10:06 pm, Joel VanderWerf removed_email_address@domain.invalid 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”


#12

On Mon, Mar 26, 2007 at 10:26:35AM +0900, Ak 756 wrote:

For example, for input.txt as

903
1047

I will get a new file as

100100000011
0001000001000111

0x903.to_s(2)
=> “100100000011”

0x1047.to_s(2)
=> “1000001000111”

And so it was:

puts IO.read(‘input.txt’).map { |x| x.to_i(16).to_s(2) }.join

_why


#13

On 3/26/07, _why removed_email_address@domain.invalid wrote:

_why

format("%0#{“193”.length*4}b", 0x193)
=> “000110010011”
to get leading zeros.

And so , the following
puts IO.read(‘input.txt’).map { |x|
format("%0#{x.chomp.length*4}b",x.to_i(16)) }.join("\n")

Prasad


#14

Phrogz wrote:

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”


#15

On 3/25/07, Ak 756 removed_email_address@domain.invalid wrote:

Mike M. 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?

Just for the record, that is commonly called BCD (for Binary Coded
Decimal).


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/