binary data

A

Alwin Blok

Hello!

I'm new to ruby. I'm learning it and it seems to be a very nice
language.

However, I do have a question about binary handling. I'd like to write
binary data directly to a file.
I found that I can use myfile.write(["1001010100"].pack("B10")), but
i'd like to be able to write, for example 0b1001010100 directly to a
file. How do I do that, or else, are ther other useful ways to write
binary data to a file

Thanks in advance

Alwin
 
A

Alwin Blok

Is my question to hard or to easy to answer? Can someone please
comment? I'm playing with ruby + fbdev on my laptop so I can make
mandelbrots everywhere I go!

Thanks,
Alwin
 
J

Jamis Buck

Alwin said:
However, I do have a question about binary handling. I'd like to
write binary data directly to a file.
I found that I can use myfile.write(["1001010100"].pack("B10")), but
i'd like to be able to write, for example 0b1001010100 directly to a
file. How do I do that, or else, are ther other useful ways to write
binary data to a file?

I'd be interested in an answer to this one, too, from someone who is "in
the know." Whenever I've needed to write binary data in the past I've
had to pack it into a string first... shouldn't there be a routine
somewhere that will write a value directly without first converting it
to a string?

Yes, I know it isn't hard to create a method that will take a value and
convert it to a binary string representation, but I'm wondering if there
is already a solution to this in the existing Ruby libraries.

- Jamis
 
D

Dave Wilson

Alwin said:
However, I do have a question about binary handling. I'd like to write
binary data directly to a file.
I found that I can use myfile.write(["1001010100"].pack("B10")), but
i'd like to be able to write, for example 0b1001010100 directly to a
file. How do I do that, or else, are ther other useful ways to write
binary data to a file?

I don't know of a more appropriate method, but what about

class IO
def raw_write bits
bits = [bits.to_s].pack('B*') if bits.is_a? Fixnum
write bits
end
end

myfile.raw_write 0b1001010100

Dave
 
Q

quillion

Alwin said:
Hello!

I'm new to ruby. I'm learning it and it seems to be a very nice language.

However, I do have a question about binary handling. I'd like to write
binary data directly to a file.
I found that I can use myfile.write(["1001010100"].pack("B10")), but i'd
like to be able to write, for example 0b1001010100 directly to a file.
How do I do that, or else, are ther other useful ways to write binary
data to a file

Thanks in advance

Alwin
very old coding style, may be of interest

begin
out_file = your data
f = File.new("file name", "w")
end

begin
out_file.each{ |ch|
f.binmode.putc(ch)
}
ensure
f.close
end



neil
 
D

daz

quillion said:
Alwin said:
Hello!

I'm new to ruby. I'm learning it and it seems to be a very nice language.

However, I do have a question about binary handling. I'd like to write
binary data directly to a file.
I found that I can use myfile.write(["1001010100"].pack("B10")), but i'd
like to be able to write, for example 0b1001010100 directly to a file.
How do I do that, or else, are ther other useful ways to write binary
data to a file

Thanks in advance

Alwin
very old coding style, may be of interest

begin
out_file = your data
f = File.new("file name", "w")
end

begin
out_file.each{ |ch|
f.binmode.putc(ch)
}
ensure
f.close
end



neil

Like Neil said ...


File.open('tbinary.dat', 'wb') do |fb|
fb.putc 0b11011011
fb.putc 255
fb.putc 0xAB
fb.putc 0x42
end

## See what happened ...
File.open('tbinary.dat', 'rb') do |fb|
bd = fb.read
puts bd.unpack('H*')
end


#-> dbffab42


daz
 

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

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top