how to tranlsate number to binary?

A

Ak 756

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?
 
H

Harold Hausman

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
 
A

Ak 756

Mike said:
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?
 
M

Mike

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?

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
 
A

Alex Gutteridge

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.to_s.split(//).map{|n| sprintf("%04d",n.to_i.to_s(2))}.join

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
H

Harold Hausman

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
 
J

Joel VanderWerf

Ak said:
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"
 
P

Patrick Hurley

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?

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
 
P

Phrogz

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

Ak 756

Gavin said:
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-:)
 
J

Joel VanderWerf

Phrogz said:
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"
 
W

_why

For example, for input.txt as

903
1047

I will get a new file as

100100000011
0001000001000111
=> "1000001000111"

And so it was:

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

_why
 
G

G.Durga Prasad

=> "1000001000111"

And so it was:

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

_why
=> "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
 
R

Rick DeNatale

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,238
Messages
2,571,193
Members
47,830
Latest member
ZacharySap

Latest Threads

Top