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
  -l,--list
     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:
  -p,--perlqq
  --htmlcref
  --xmlcref

See also: Perl: Unicode Tutorial 🐪

If you have a question, put $5 at patreon and message me.

Perl

  1. Perl Overview
  2. Version String
  3. Help System

Detail

  1. Quoting String
  2. Format String
  3. String Operations
  4. True, False
  5. if then else
  6. Loop
  7. List / Array
  8. Loop Thru List
  9. Map f to List
  10. List Comprehension
  11. Hash Table
  12. Function Optional Param
  13. regex
  14. Split Line by Regex

Advanced

  1. Sort List, Matrix, Object
  2. Sort Matrix
  3. List Modules, Search Paths
  4. Write a Module
  5. Read / Write File
  6. Traverse Dir
  7. Find / Replace
  8. Complex Numbers
  9. System Call
  10. Compress / Decompress gzip
  11. Get Env Var
  12. GET Web Content
  13. Unicode 🐪
  14. Convert File Encoding
  15. Sort Unstable