Helo
I have method that read a binary file - maybe a image, and return a byte
array.
I want write the byte array to another file. But I keep writing the
bytes as
text. I want binary.
What need I do?
Rajesh.
Helo
I have method that read a binary file - maybe a image, and return a byte
array.
I want write the byte array to another file. But I keep writing the
bytes as
text. I want binary.
What need I do?
Rajesh.
Rajesh Olafson wrote:
Rajesh.
I’m guessing that you’re on Windows. Open the file with the ‘b’ flag,
i.e. ‘wb’.
Rajesh.
I’m guessing that you’re on Windows. Open the file with the ‘b’ flag, i.e.
‘wb’.
Thank you.
no. I use linux ubuntu. shold have said that before.
With my file write I just get text in the file. Not binary.
?
Rajesh.
Rajesh Olafson wrote:
Then I don’t understand. What do you mean you “just get text”? If you’re
on Linux or you’ve opened the file with the b flag, then you should get
the data you write w/o conversion.
Maybe it would help if you showed an example of your code.
Hello Tim,
I do like
file_name=new_foo.gif
my_byte_array = myMethod(‘foo.gif’)
puts my_byte_array.class # => Array
outfile = File.new(‘new_foo.gif’, ‘w’)
outfile.puts(byteArray)
outfile.close
now outfile only had some nonsense text.
ascii text
?
Thank you.
Rajesh.
byteArray.each_byte do | byte |
ooops make this
byteArray.each
of course (too many bytes make me dizzy
File.open( ‘new_foo.gif’, ‘w’ ) do |output|
byteArray.each_byte do | byte |
output.print byte.chr
end
end
http://ruby-smalltalk.blogspot.com/
As simple as possible, but not simpler.
Albert Einstein
Thank you Robert. it work now.
Rajesh.
On Thu, Jun 12, 2008 at 3:51 AM, Robert D. removed_email_address@domain.invalid
Hi all, sorry to reopen this issue, I’m facing something weird trying to
reaseemble a binary stream to file. I tried the same code you are using
here:
File.open(‘E:\UpdatePackage\Materials\coco2.jpg’, ‘w’ ) do |output|
a.each do | byte |
output.print byte.chr
end
end
print File.size(‘E:\UpdatePackage\Materials\coco2.jpg’), “\n” # out
1190386
print a.length, “\n” # out 1185939
which is supposed to work fine, BUT the reaasembled file is not valid
and is not the same length in bytes than the array length:
array ‘a’ length: 1185939
new file length: 1190386
For me, this is weird b/c i’m passing exactly all those 1185939 bytes to
file, but the new file is 1190386.
Any idea?
Thanks in advance
Rajesh Olafson wrote:
Thank you Robert. it work now.
Rajesh.On Thu, Jun 12, 2008 at 3:51 AM, Robert D. removed_email_address@domain.invalid
Alexis Alulema wrote:
File.open(‘E:\UpdatePackage\Materials\coco2.jpg’, ‘w’ ) do |output|
a.each do | byte |
output.print byte.chr
end
end
Open the file with “wb”’.
Cool, that was great and it finally worked. Thank u very much.
Tim H. wrote:
Alexis Alulema wrote:
File.open(‘E:\UpdatePackage\Materials\coco2.jpg’, ‘w’ ) do |output|
a.each do | byte |
output.print byte.chr
end
endOpen the file with “wb”’.
On Dec 22, 2010, at 14:18 , Aegorov E. wrote:
i’m get error
undefined method `chr’ for “255”:String (NoMethodError)
with this codeFile.open( filename, ‘wb’ ) do |output|
new_array.each do | byte |
output.print byte.chr
end
end
you have an array of strings of numbers:
[ “255” ].each do |not_a_byte|
…
end
i’m get error
undefined method `chr’ for “255”:String (NoMethodError)
with this code
File.open( filename, ‘wb’ ) do |output|
new_array.each do | byte |
output.print byte.chr
end
end
On Dec 22, 2010, at 14:40 , Aegorov E. wrote:
i get error
in `chr’: 337 out of char range (RangeError)
337 isn’t a valid 8-bit byte. aka 337 > 255.
What are you actually trying to do? You have too many 1’s for me to
guess.
I realized my mistake
i’m write small bmp files аrchiver.
Thank you, Ryan D. .
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs