Write byte array to file

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:

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

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

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

···

--
Posted via http://www.ruby-forum.com/.

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]

···

--
Posted via http://www.ruby-forum.com/.

I realized my mistake
i'm write small bmp files аrchiver.

···

--
Posted via http://www.ruby-forum.com/.

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

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.

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

you have an array of strings of numbers:

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

···

On Dec 22, 2010, at 14:18 , Aegorov Egorov wrote:

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

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.

···

On Dec 22, 2010, at 14:40 , Aegorov Egorov wrote:

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

Rajesh Olafson wrote:

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

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.

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

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.

On Wed, Jun 11, 2008 at 6:34 PM, Tim Hunter <TimHunter@nc.rr.com> wrote:

Rajesh Olafson wrote:

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

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.

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

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

HTH
Robert

···

--
http://ruby-smalltalk.blogspot.com/

---
As simple as possible, but not simpler.
Albert Einstein

    byteArray.each_byte do | byte |

ooops make this
         byteArray.each
of course (too many bytes make me dizzy :wink:

Thank you Robert. it work now.
Rajesh.

···

On Thu, Jun 12, 2008 at 3:51 AM, Robert Dober <robert.dober@gmail.com> wrote:

> byteArray.each_byte do | byte |
ooops make this
        byteArray.each
of course (too many bytes make me dizzy :wink:

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 Dober <robert.dober@gmail.com>

--
Posted via http://www.ruby-forum.com/\.

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"'.

···

--
Posted via http://www.ruby-forum.com/\.

Cool, that was great and it finally worked. Thank u very much.

Tim Hunter wrote:

···

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"'.

--
Posted via http://www.ruby-forum.com/\.