iconv默認情況下,是沒有被安裝的,下面簡單介紹下iconv的安裝過程:
1. 下載:
http://www.gnu.org/software/libiconv/#TOCdownloading
2. 安裝:
下載完成後,切換到下載目錄先進行解壓:
- $tar -xzvf libiconv-1.14.tar.gz
然後進入解壓後的文件中
- $cd libiconv-1.14_2
- $ ./configure --prefix=/usr/local
- $ make
- $ make install
該工具安裝完成後,肯定要先了解下這個命令的用法吧,這個沒什麼可說的:
- $iconv --help
- Usage: iconv [OPTION...] [FILE...]
- Convert encoding of given files from one encoding to another.
- Input/Output format specification:
- -f, --from-code=NAME encoding of original text
- -t, --to-code=NAME encoding for output
- Information:
- -l, --list list all known coded character sets
- Output control:
- -c omit invalid characters from output
- -o, --output=FILE output file
- -s, --silent suppress warnings
- --verbose print progress information
- -?, --help Give this help list
- --usage Give a short usage message
- -V, --version Print program version
- Mandatory or optional arguments to long options are also mandatory or optional
- for any corresponding short options.
iconv -f 原編碼 -t 目標編碼 要轉換的文件
4. 編碼轉換:
學會了編碼的轉化,我們就舉了例子示範一下:
- $iconv -f gbk -t utf8 test.txt
- <span style="font-family:宋體; font-size:14px">執行下面的命令:</span>
- $iconv -f gbk -t utf8 test.txt > test.convert.txt
注意:
如果不出意外的話,上面的安裝步驟可沒有那麼順利,在make的時候,會提示下面的錯誤:
- n file included from progname.c:26:0:
- ./stdio.h:1010:1: error: ‘gets’ undeclared here (not in a function)
- _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
- ^
- make[2]: *** [progname.o] Error 1
- make[2]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'
- make[1]: *** [all] Error 2
- make[1]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'
- make: *** [all] Error 2
- --- srclib/stdio.in.h.orig 2011-08-07 16:42:06.000000000 +0300
- +++ srclib/stdio.in.h 2013-01-10 15:53:03.000000000 +0200
- @@ -695,7 +695,9 @@
- /* It is very rare that the developer ever has full control of stdin,
- so any use of gets warrants an unconditional warning. Assume it is
- always declared, since it is required by C89. */
- -_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
- +#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
- + _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
- +#endif
- #endif
那我們只要進行如下操作即可解決這個問題:
1. 切換到srclib目錄下:
- $cd srclib
2. 修改stdio.in.h文件:
- $gedit stdio.in.h
- #if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
- _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
- #endif