# Iconv problems with different machines

**URL:** <https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185>\
**Category:** Ruby\
**Created:** [December 5, 2007, 11:25am UTC](https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185 "2007-12-05T11:25:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [December 5, 2007, 11:25am UTC](https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185/1 "2007-12-05T11:25:19Z")

</div>

Hi,

I have the following piece of code:

ic = Iconv.new(‘US-ASCII//TRANSLIT’, ‘UTF-8’)  
puts ic.iconv(“AÃ¼thor”)

1. on my local machine (OSX 10.5) when I run this, I get the output:  
A"uthor

2. when I run this same code on my debian server (via rake executed  
through a capistrano task) I get the output: A?thor

3. when I run this same code on my debian server (via irb), I get:  
Author

Both 1 and 3 are acceptable output to me, however I cant figure out how  
to get my program to output the correct result on my server when I run  
it through a capistrano task. Is there some environment variable I need  
to set? From reading other posts, I’ve tried adding at the top of my  
file:  
$KCODE = “u”  
require ‘jcode’  
ENV[‘LANG’] = ‘en\_US.UTF-8’  
ENV[‘LC\_CTYPE’] = ‘en\_US.UTF-8’

still doesn’t fix the issue. Any help would be greatly appreciated.

Thanks,  
Ray

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [December 5, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185/2 "2007-12-05T12:14:17Z")

</div>

Actually I found some other posts about this same issue from awhile  
ago… Appears there’s no solution.

I stopped using the iconv library and instead switched to the iconv  
system command and that seems to work. Not the best solution, but at  
least it works…

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [December 5, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185/3 "2007-12-05T12:28:29Z")

</div>

On Dec 5, 2007, at 12:14 PM, Raymond O’Connor wrote:

> Actually I found some other posts about this same issue from awhile  
> ago… Appears there’s no solution.
> 
> I stopped using the iconv library and instead switched to the iconv  
> system command and that seems to work. Not the best solution, but at  
> least it works…

I have not been able to understand where is exactly the difference,  
but looks like depending on the system/version/something the  
transliteration tables are just different. At ASPgems we wrote this  
hand-crafted normalizer which we know is portable for sure (note that  
it uses Rails #chars and does a bit more stuff, but you see the idea):

def self.normalize(str)  
return ‘’ if str.nil?  
n = str.chars.downcase.strip.to\_s  
n.gsub!(/[Ã Ã¡Ã¢Ã£Ã¤Ã¥ÄÄƒ]/, ‘a’)  
n.gsub!(/Ã¦/, ‘ae’)  
n.gsub!(/[ÄÄ‘]/, ‘d’)  
n.gsub!(/[Ã§Ä‡ÄÄ‰Ä‹]/, ‘c’)  
n.gsub!(/[Ã¨Ã©ÃªÃ«Ä“Ä™Ä›Ä•Ä—]/, ‘e’)  
n.gsub!(/Æ’/, ‘f’)  
n.gsub!(/[ÄÄŸÄ¡Ä£]/, ‘g’)  
n.gsub!(/[Ä¥Ä§]/, ‘h’)  
n.gsub!(/[Ã¬Ã¬Ã­Ã®Ã¯Ä«Ä©Ä­]/, ‘i’)  
n.gsub!(/[Ä¯Ä±Ä³Äµ]/, ‘j’)  
n.gsub!(/[Ä·Ä¸]/, ‘k’)  
n.gsub!(/[Å‚Ä¾ÄºÄ¼Å€]/, ‘l’)  
n.gsub!(/[Ã±Å„ÅˆÅ†Å‰Å‹]/, ‘n’)  
n.gsub!(/[Ã²Ã³Ã´ÃµÃ¶Ã¸ÅÅ‘ÅÅ]/, ‘o’)  
n.gsub!(/Å“/, ‘oe’)  
n.gsub!(/Ä…/, ‘q’)  
n.gsub!(/[Å•Å™Å—]/, ‘r’)  
n.gsub!(/[Å›Å¡ÅŸÅÈ™]/, ‘s’)  
n.gsub!(/[Å¥Å£Å§È›]/, ‘t’)  
n.gsub!(/[Ã¹ÃºÃ»Ã¼Å«Å¯Å±Å­Å©Å³]/, ‘u’)  
n.gsub!(/Åµ/, ‘w’)  
n.gsub!(/[Ã½Ã¿Å·]/, ‘y’)  
n.gsub!(/[Å¾Å¼Åº]/, ‘z’)  
n.gsub!(/\s+/, ’ ‘)  
n.delete!(’^ a-z0-9\_/\-’)  
n  
end

– fxn

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [December 5, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185/4 "2007-12-05T22:44:05Z")

</div>

Raymond O’Connor said…

> 1. when I run this same code on my debian server (via rake executed  
> $KCODE = “u”  
> require ‘jcode’  
> ENV[‘LANG’] = ‘en\_US.UTF-8’  
> ENV[‘LC\_CTYPE’] = ‘en\_US.UTF-8’
> 
> still doesn’t fix the issue. Any help would be greatly appreciated.

I’ve found a lot of bugs with the MRI Iconv and now only use it with  
JRuby - which, I suspect, uses the Java SE convertors.

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [December 5, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/iconv-problems-with-different-machines/122185/5 "2007-12-05T21:05:59Z")

</div>

Hi Xavier,

I like that solution even better. Thanks for sharing!

Best,  
Ray
