Perl: Convert File Encoding

By Xah Lee. Date: . Last updated: .

How to convert a file encoding in Perl?

Use the shell util /usr/bin/piconv.

# convert utf16 encoding to utf8
piconv -f UTF-16 -t utf8 file.txt > filenew.txt

Type piconv for help.

piconv is installed by Perl and written in Perl. You can also look at the code to see how it's done.

◆ piconv
piconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
piconv -l
piconv -r encoding_alias
     lists all available encodings
  -r,--resolve encoding_alias
    resolve encoding to its (Encode) canonical name
  -f,--from from_encoding
     when omitted, the current locale will be used
  -t,--to to_encoding
     when omitted, the current locale will be used
  -s,--string string
     "string" will be the input instead of STDIN or files
The following are mainly of interest to Encode hackers:
  -D,--debug          show debug information
  -C N | -c           check the validity of the input
  -S,--scheme scheme  use the scheme for conversion
Those are handy when you can only see ascii characters:

See also: Perl: Unicode Tutorial 🐪