# Writing encoded images to a file

**URL:** <https://www.ruby-forum.com/t/writing-encoded-images-to-a-file/62417>\
**Category:** Ruby\
**Created:** [June 4, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/writing-encoded-images-to-a-file/62417 "2006-06-04T14:24:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [June 4, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/writing-encoded-images-to-a-file/62417/1 "2006-06-04T14:24:51Z")

</div>

Word 2003 supports an xml output for documents that uses Base64 to  
encode the embedded images. Im looking to grab that image out of the  
xml and write it to a file. Im able to parse the xml and isolate the  
Base64 string but decoding it out to a file hasn’t been working for me;  
the image doesn’t come back looking correct. Stripped down version of  
the code assuming ‘AaBaCc’ represents the really long Base64 encoded GIF  
image string:

require ‘base64’  
str = ‘AaBbCc’  
file = File.new("/test.gif", “w”)  
file.syswrite(Base64.decode64(str))

Could be the string coming in, but wanted to get an idea if Im headed in  
the right direction with the output before I tackle that debug  
nightmare.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [June 4, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/writing-encoded-images-to-a-file/62417/2 "2006-06-04T14:34:18Z")

</div>

On Sun, 2006-06-04 at 21:25 +0900, Aaron Worsham wrote:

> str = ‘AaBbCc’  
> file = File.new("/test.gif", “w”)  
> file.syswrite(Base64.decode64(str))

Assuming you’re on Windows, you’ll probably want “wb” rather than just  
“w” for the file mode. Also, I’d do this:

File.open("/test.gif", “wb”) do |file|  
file \<\< Base64.decode64(str)  
end

to make sure the file gets closed.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [June 4, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/writing-encoded-images-to-a-file/62417/3 "2006-06-04T14:34:29Z")

</div>

You could try change:  
file = File.new(“/test.gif”, “w”)  
to  
file = File.new(“/test.gif”, “wb”)  
So that its in binary mode.

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

c,y=(r,p,s=%w(rock paper scissors))[rand 3];d={s,  
{p,0,r,1},p,{r,1,s,0},r,{s,1,p,0}};until y=~/(#{r  
}|#{s}|#{p})/;$\>\<\<“#{[r,s]_", "} #{[“or”,p]_” "}?  
";y=gets.chomp;end;$\>\<\<“You:#{y}\nComp:#{c}\n”;$\>  
\<\<“You #{d[y][c]?(d[y][c]\>0?“Win”:“Lose”):“Draw”}\n”

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [June 4, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/writing-encoded-images-to-a-file/62417/4 "2006-06-04T15:00:40Z")

</div>

Ross B. wrote:

> On Sun, 2006-06-04 at 21:25 +0900, Aaron Worsham wrote:
> 
> > str = ‘AaBbCc’  
> > file = File.new("/test.gif", “w”)  
> > file.syswrite(Base64.decode64(str))
> 
> Assuming you’re on Windows, you’ll probably want “wb” rather than just  
> “w” for the file mode. Also, I’d do this:
> 
> File.open("/test.gif", “wb”) do |file|  
> file \<\< Base64.decode64(str)  
> end
> 
> to make sure the file gets closed.

That worked. Thank you very much.
