Alex Rivera | Logout

Best way to convert text files between character sets?

Asked 2008-09-15T17:21:31.133
627

What is the fastest, easiest tool or method to convert text files between character sets?

Specifically, I need to convert from UTF-8 to ISO-8859-15 and vice versa.

Everything goes: one-liners in your favorite scripting language, command-line tools or other utilities for OS, web sites, etc.

Best solutions so far:

On Linux/UNIX/OS X/cygwin:

  • Gnu iconv suggested by Troels Arvin is best used as a filter. It seems to be universally available. Example:

      $ iconv -f UTF-8 -t ISO-8859-15 in.txt > out.txt
    

    As pointed out by Ben, there is an online converter using iconv.

  • recode (manual) suggested by Cheekysoft will convert one or several files in-place. Example:

      $ recode UTF8..ISO-8859-15 in.txt
    

    This one uses shorter aliases:

      $ recode utf8..l9 in.txt
    

    Recode also supports surfaces which can be used to convert between different line ending types and encodings:

    Convert newlines from LF (Unix) to CR-LF (DOS):

      $ recode ../CR-LF in.txt
    

    Base64 encode file:

      $ recode ../Base64 in.txt
    

    You can also combine them.

    Convert a

Edit
Report

3 Answers

24

iconv(1)

iconv -f FROM-ENCODING -t TO-ENCODING file.txt

Also there are iconv-based tools in many languages.

answered 2008-09-15T17:23:17.183
19

Try iconv Bash function

I've put this into .bashrc:

utf8()
{
    iconv -f ISO-8859-1 -t UTF-8 $1 > $1.tmp
    rm $1
    mv $1.tmp $1
}

..to be able to convert files like so:

utf8 MyClass.java
answered 2011-12-06T14:43:58.560
3

PHP iconv()

iconv("UTF-8", "ISO-8859-15", $input);

answered 2008-09-17T06:18:34.940

Your Answer