iconv是一个计算机程序以及一套应用程序编程接口的名称。它的作用是在多种国际编码格式之间进行文本内码的转换。作为应用程序的iconv采用命令行界面,允许将某种特定编码的文件转换为另一种编码。
NAME iconv - convert text from one character encoding to another SYNOPSIS iconv [options] [-f from-encoding] [-t to-encoding] [inputfile]... #-f from-encoding, --from-code=from-encoding # Use from-encoding for input characters. #-t to-encoding, --to-code=to-encoding # Use to-encoding for output characters. #-l, --list # List all known character set encodings. #-o outputfile, --output=outputfile # Use outputfile for output.
#创建一个文件hello.c,测试。 [ycxie@fedora Workspace]$ vim hello.c [ycxie@fedora Workspace]$ file hello.c hello.c: C source, UTF-8 Unicode text #UTF-8编码,显示正常。 [ycxie@fedora Workspace]$ cat hello.c #include <stdio.h> int main(void) { printf("燕京寓客"); return 0; } #用iconv命令转换文件编码。 [ycxie@fedora Workspace]$ iconv -f UTF-8 -t GBK hello.c -o hello.c [ycxie@fedora Workspace]$ file hello.c hello.c: C source, ISO-8859 text #文件hello.c显示乱码。 [ycxie@fedora Workspace]$ cat hello.c #include <stdio.h> int main(void) { printf("�ྩԢ��"); return 0; } #将文件hello.c文件编码再转换成UTF-8。 [ycxie@fedora Workspace]$ iconv -f GBK -t UTF-8 hello.c -o hello.c
iconv基于GPL公开源代码,是GNU项目的一部分,在各种UNIX操作系统下均可使用。目前,libiconv已经包含在C运行时刻库libc.so中。因此,Linux平台上使用iconv库函数的程序,需要包含