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?
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 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.
On 3/25/07, Harold Hausman <hhausman@gmail.com> wrote:
On 3/26/07, Ak 756 <macro.peng@gmail.com> wrote:
> 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:
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
···
On 3/26/07, _why <why@ruby-lang.org> wrote:
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
>
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?
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?
On Mar 26, 1:32 pm, Ak 756 <macro.p...@gmail.com> wrote:
Mike Moore 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?
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?
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?
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?
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):002:0> 903.to_s.split('').map{|n| "%04b" % n }.to_s
=> "100100000011"
···
On Mar 25, 10:06 pm, Joel VanderWerf <v...@path.berkeley.edu> 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?
On Mar 25, 10:06 pm, Joel VanderWerf <v...@path.berkeley.edu> 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?
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):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: