這幾天頻繁在windows和ubuntu下調試代碼,字符編碼格式不同老是造成很大的困擾,windows下的默認字符編碼格式是GBK(gb2312)而在ubuntu下的是utf-8。前天光來回的保存編碼格式就費了很大的勁,今天google了下找到了個臨時還算是不錯的方法。
ubuntu下自帶一個字符編碼格式調整命令:iconv
ICONV(1) Debian GNU/Linux ICONV(1)
NAME
iconv - Convert encoding of given files from one encoding to another
SYNOPSIS
iconv -f encoding [-t encoding] [inputfile]...
DESCRIPTION
The iconv program converts the encoding of characters in inputfile, or
from the standard input if no filename is specified, from one coded
character set to another. The result is written to standard output
unless otherwise specified by the --output option.
--from-code, -f encoding
Convert characters from encoding.
--to-code, -t encoding
Convert characters to encoding. If not specified the encoding
corresponding to the current locale is used.
--list, -l
List known coded character sets.
-c Omit invalid characters from output.
--output, -o file
Specify output file (instead of stdout).
--silent, -s
Suppress warnings, but not errors.
--verbose
Print progress information.
--help, -?
Give help list.
--usage
Give a short usage message.
--version, -V
Print program version.
ENCODINGS
The values permitted for --from-code and --to-code can be listed by the
iconv --list command, and all combinations of the listed values are
supported. Furthermore the following two suffixes are supported:
//TRANSLIT
When the string "//TRANSLIT" is appended to --to-code,
transliteration is activated. This means that when a character
cannot be represented in the target character set, it can be
approximated through one or several similarly looking
characters.
//IGNORE
When the string "//IGNORE" is appended to --to-code, characters
that cannot be represented in the target character set will be
silently discarded.
AUTHOR
iconv was written by Ulrich Drepper as part of the GNU C Library.
This man page was written by Joel Klecker <[email protected]>, for the
Debian GNU/Linux system.
3rd Berkeley Distribution lenny ICONV(1)
通過上面的介紹估計大家就可以很好的理解了。
iconv -f gb2312 -t utf-8 a.txt > b.txt;
就可以將字符編碼爲gb2312的文件a.txt轉換到編碼格式爲utf-8的b.txt文件。這樣就可以在ubuntu下正常的查看了。希望可以幫到大家,自己也留底備用。
如果是在windows下,可以使用word來修改字符編碼,但是比較麻煩,有一個小軟件:convert比較好用,能批量式修改。大家可以搜一下。我也上傳了一個到資源區,大家可以下載:http://download.csdn.net/detail/timebomb/3987420