# Bug in String#unpack on windows or?

**URL:** <https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711>\
**Category:** Ruby\
**Created:** [June 13, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711 "2007-06-13T15:11:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 13, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711/1 "2007-06-13T15:11:51Z")

</div>

I am trying to save attachment(s) from mail read with net/imap.  
Attachment is a tif file.

att = imap.fetch(msgID, “BODY[2]”)[0].attr[“BODY[2]”].unpack(‘m’)  
File.new(‘att.tif’,‘w+’).write(att)

I tried it on linux and it works withouth glitch, but on windows every  
LF (x0A) character is replaced with CRLF sequence. Needless to say file  
is unreadable.

Is this a bug or am I missing something?

Thank you

TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 13, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711/2 "2007-06-13T15:21:08Z")

</div>

Damjan R. wrote:

> Is this a bug or am I missing something?  
> Try File.new(‘att.tif’,‘wb+’).write(att). From memory, the ‘b’ in ‘wb+’  
> turns off newline conversion.

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 13, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711/3 "2007-06-13T15:31:10Z")

</div>

Le 13 juin à 15:20, Alex Y. a écrit :

> Damjan R. wrote:

> > Is this a bug or am I missing something?
> 
> Try File.new(‘att.tif’,‘wb+’).write(att). From memory, the ‘b’ in ‘wb+’  
> turns off newline conversion.

Yep. From the PickAxe :

Mode Meaning  
r Read-only, starts at beginning of file (default mode).  
r+ Read/write, starts at beginning of file.  
w Write-only, truncates an existing file to zero length or creates  
a new file for writing.  
w+ Read/write, truncates existing file to zero length or creates a  
new file for reading and writing.  
a Write-only, starts at end of file if file exists ; otherwise  
creates a new file for writing.  
a+ Read/write, starts at end of file if file exists ; otherwise  
creates a new file for reading and writing.  
b (DOS/Windows only) Binary file mode (may appear with any of the  
key letters listed above).

I’d add that b has no effect whatsoever on other OSes. (In other words,  
you may leave “wb+” and stay platform independent.)

Fred

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 13, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711/4 "2007-06-13T15:42:12Z")

</div>

On 13.06.2007 15:28, F. Senault wrote:

> Mode Meaning  
> b (DOS/Windows only) Binary file mode (may appear with any of the  
> key letters listed above).
> 
> I’d add that b has no effect whatsoever on other OSes. (In other words,  
> you may leave “wb+” and stay platform independent.)

That’s what I would do for binary files. It also helps documenting.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 14, 2007, 7:31am UTC](https://www.ruby-forum.com/t/bug-in-string-unpack-on-windows-or/106711/5 "2007-06-14T07:31:07Z")

</div>

Yep. Thats it. Thank you guys.

by  
TheR
