perl 編碼
utf8 與 big5 編碼轉換
- 將 big5 資料格式的檔案轉為 utf8 格式
my $utf_data = encode("utf8", decode("big5", $data)); # $data為big5格式, $utf_data為utf8格式
- utf8 編碼下顯示 big5 資料文件
from_to($data, "big5", "utf8"); # $data從big5格式轉為utf8格式
- 如何直接生成 utf8 格式的資料文件
use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use Encode qw/encode decode from_to/; my $cgi = new CGI; # charset utf8 print $cgi->header(-type=>'text/html',-charset=>'utf-8'); # open the big5 file open(FH, "big5.dat"); my $data = ; close(FH); # convert big5 to utf8 my $utf_data = encode("utf8", decode("big5", $data)); # produce the utf8 file open(FH, ">utf8.dat"); print FH $utf_data; close(FH); my $word = "繁體中文"; from_to($word, "big5", "utf8"); print "$utf_data, $word";
- 不涉及文件讀取,script文件裡print "中文"
use CGI qw/:standard/; use encoding "euc-tw", STDOUT => "utf8"; my $cgi = new CGI; print $cgi->header(-type=>'text/html',-charset=>'utf-8'); print "台灣霹靂火";