write byte array to file

R

Rajesh Olafson

[Note: parts of this message were removed to make it a legal post.]

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

Tim Hunter

Rajesh said:
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.

I'm guessing that you're on Windows. Open the file with the 'b' flag,
i.e. 'wb'.
 
R

Rajesh Olafson

[Note: parts of this message were removed to make it a legal post.]
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.

 
T

Tim Hunter

Rajesh said:
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.
?

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

Rajesh Olafson

[Note: parts of this message were removed to make it a legal post.]

Hello Tim,

I do like

file_name=new_foo.gif

# myMethod return a byte array for file_name
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.

# file new_foo.gif
ascii text

?

Thank you.

Rajesh.
 
R

Robert Dober

File.open( 'new_foo.gif', 'w' ) do |output|
byteArray.each_byte do | byte |
output.print byte.chr
end
end

HTH
Robert
 
R

Robert Dober

byteArray.each_byte do | byte |
ooops make this
byteArray.each
of course (too many bytes make me dizzy ;)
 
R

Rajesh Olafson

[Note: parts of this message were removed to make it a legal post.]

Thank you Robert. it work now.
Rajesh.
 
A

Alexis Alulema

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 said:
Thank you Robert. it work now.
Rajesh.
--
Posted via http://www.ruby-forum.com/.
 
T

Tim Hunter

Alexis said:
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"'.
 
A

Aegorov Egorov

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
 
R

Ryan Davis

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

you have an array of strings of numbers:

[ "255" ].each do |not_a_byte|
...
end
 
A

Aegorov Egorov

after
array = new_array.map{ |i| i.to_i }
File.open( filename, 'wb' ) do |output|
array.each do | byte |
output.print byte.chr
end
end

i get error
in `chr': 337 out of char range (RangeError)

p array

[1, 255, 1, 216, 1, 255, 1, 224, 1, 0, 1, 16, 1, 74, 1, 70, 1, 73, 1,
70, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 1, 1, 2, 0, 1, 255, 1, 219, 1, 0, 1,
132, 1, 0, 1, 9, 2, 6, 1, 8, 1, 6, 1, 2, 1, 9, 1, 8, 1, 7, 1, 8, 1, 10,
2, 9, 1, 10, 1, 2, 1, 10, 5, 2, 1, 14, 1, 7, 1, 8, 1, 5, 1, 10, 1, 19,
1, 16, 1, 21, 1, 20, 1, 19, 1, 16, 2, 18, 1, 23, 1, 27, 1, 38, 1, 30, 1,
23, 1, 25, 1, 35, 1, 25, 2, 18, 1, 31, 1, 47, 1, 32, 1, 35, 1, 39, 1,
41, 3, 44, 1, 21, 1, 30, 1, 49, 1, 53, 1, 48, 1, 42, 1, 53, 1, 38, 1,
43, 1, 44, 1, 41, 1, 1, 1, 9, 2, 10, 3, 5, 1, 13, 2, 5, 1, 13, 1, 41, 1,
24, 1, 18, 1, 24, 50, 41, 1, 255, 1, 192, 1, 0, 1, 17, 1, 8, 1, 0, 1,
225, 1, 0, 1, 225, 1, 3, 1, 1, 1, 34, 1, 0, 1, 2, 1, 17, 1, 1, 1, 3, 1,
17, 1, 1, 1, 255, 1, 196, 1, 0, 1, 21, 1, 0, 2, 1, 15, 0, 1, 7, 1, 255,
1, 196, 1, 0, 1, 20, 1, 16, 1, 1, 16, 0, 1, 255, 1, 196, 1, 0, 1, 20, 2,
1, 16, 0, 1, 255, 1, 196, 1, 0, 1, 20, 1, 17, 1, 1, 16, 0, 1, 255, 1,
218, 1, 0, 1, 12, 1, 3, 1, 1, 1, 0, 1, 2, 1, 17, 1, 3, 1, 17, 1, 0, 1,
63, 1, 0, 1, 135, 337, 0, 1, 15, 1, 255, 1, 217]
 
R

Ryan Davis

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top