# US-ASCII to UTF-8

**URL:** <https://www.ruby-forum.com/t/us-ascii-to-utf-8/184846>\
**Category:** Ruby\
**Created:** [March 9, 2010, 1:50pm UTC](https://www.ruby-forum.com/t/us-ascii-to-utf-8/184846 "2010-03-09T13:50:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [March 9, 2010, 1:50pm UTC](https://www.ruby-forum.com/t/us-ascii-to-utf-8/184846/1 "2010-03-09T13:50:41Z")

</div>

I’m having trouble in with us-ascii strings. I have written a script  
which receives emails straight from postfix. The emails are generally  
encoded in “iso-8859-1”, and I can’t get all of the characters to  
display properly, and I can’t save to my mongodb database, either. Here  
is an example problem string:

In irb by default it displays like this:

Geschï¿½ftsfï¿½hrer

If I set $KCODE = ‘iso-8859-1’ then it gets a bit better:

Gesch\344ftsf\374hrer

But how do I now make that into the correct string:

GeschÃ¤ftsfÃ¼hrer

I have tried Iconv:

Iconv.iconv(‘iso-8859-1’, ‘utf-8’, string)

Iconv::IllegalSequence: “\344ftsf\374hrer”

Please help!

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [March 9, 2010, 2:07pm UTC](https://www.ruby-forum.com/t/us-ascii-to-utf-8/184846/2 "2010-03-09T14:07:23Z")

</div>

Matt B. wrote:

> Iconv.iconv(‘iso-8859-1’, ‘utf-8’, string)
> 
> Iconv::IllegalSequence: “\344ftsf\374hrer”

You just got the args the wrong way round. ‘to’ comes before ‘from’.  
(ri Iconv.iconv)

> > RUBY\_VERSION  
> > =\> “1.8.7”
> 
> > require ‘iconv’  
> > =\> true
> 
> > string = “Gesch\344ftsf\374hrer”  
> > =\> “Gesch\344ftsf\374hrer”
> 
> > puts Iconv.iconv(‘utf-8’, ‘iso-8859-1’, string).first  
> > GeschÃ¤ftsfÃ¼hrer

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [March 9, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/us-ascii-to-utf-8/184846/3 "2010-03-09T14:19:57Z")

</div>

Brian C. wrote:

> Matt B. wrote:
> 
> > Iconv.iconv(‘iso-8859-1’, ‘utf-8’, string)
> > 
> > Iconv::IllegalSequence: “\344ftsf\374hrer”
> 
> You just got the args the wrong way round. ‘to’ comes before ‘from’.  
> (ri Iconv.iconv)
> 
> > > RUBY\_VERSION  
> > > =\> “1.8.7”
> > 
> > > require ‘iconv’  
> > > =\> true
> > 
> > > string = “Gesch\344ftsf\374hrer”  
> > > =\> “Gesch\344ftsf\374hrer”
> > 
> > > puts Iconv.iconv(‘utf-8’, ‘iso-8859-1’, string).first  
> > > GeschÃ¤ftsfÃ¼hrer

lol, ok, now I feel stupid. I’ve been messing around with this for  
hours! Thanks very much.
