參考這一篇 UTF8 和 BIG5 轉碼的做法,一直出現錯誤訊息。
Cannot decode string with wide characters at C:/Perl/lib/Encode.pm line 176.
查到另一篇提到,可以不用 decode、直接 encode,搞定。
測試1:
UTF8 的來源是文字檔,必須 decode 再 encode 才行。
use Encode;
open OUT, '>', 'big5.txt';
open IN, '<', 'utf8.txt';
while ($line = <IN>) {
$line = encode('big5', decode('utf8',$line));
print OUT $line;
}
close IN;
close OUT;
測試2:
UTF8 的來源是SQL2008, 不用 decode、直接 encode。
use DBI;
use Encode;
my $dbs = "dbi:ODBC:DRIVER={SQL Server};SERVER={MyServer}";
my ($username, $password) = ('MyAccount', 'MyPassword');
my $dbh = DBI->connect($dbs, $username, $password);
$sql_command = "select * from MyTable";
$sth = $dbh->prepare($sql_command);
$sth->execute();
@result = $sth->fetchrow_array;
$string0 = Encode::encode('big5', $result[0]);
$string1 = Encode::encode('big5', $result[1]);
open FH_o, ">", $csvfile;
print FH_o "$string0, $string1\n";
close FH_o;
沒有留言:
張貼留言