[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

write byte array to file

Rajesh Olafson

6/11/2008 7:24:00 PM

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

7 Answers

Tim Hunter

6/11/2008 9:31:00 PM

0

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.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Rajesh Olafson

6/11/2008 10:08:00 PM

0

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

>
>> 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.ruby...
> RMagick 2: http://rmagick.ruby...rmagick2.html
>
>

Tim Hunter

6/11/2008 10:34:00 PM

0

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.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Rajesh Olafson

6/12/2008 2:09:00 AM

0

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

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.ruby...
> RMagick 2: http://rmagick.ruby...rmagick2.html
>
>

Robert Dober

6/12/2008 7:49:00 AM

0

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

HTH
Robert
--
http://ruby-smalltalk.blo...

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

Robert Dober

6/12/2008 7:51:00 AM

0

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

Rajesh Olafson

6/13/2008 2:54:00 PM

0

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

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 ;)
>
>