Re: [問題] 中文utf-8與big5互轉的問題
※ 引述《plokijuh (瘋人院院長)》之銘言:
: 是這樣的
: 我需要把原本是 utf-8 的編碼轉成 big5 後 再轉回 utf-8 的文字
: 結果我發現這樣的互轉會有問題
: OS: Windows XP SP3
: JDK: 1.6.0 update 11
: IDE: Eclipse 3.4
: Eclipse設定
: Genel>Workspace: UTF-8
: Project>Resource:UTF-8
: import java.io.UnsupportedEncodingException;
: public class TestRun {
: public static void main(String[] args) throws UnsupportedEncodingException
: {
: String enc = System.getProperty("file.encoding");
: System.out.println(enc);
: String e = "中文轉碼測試";
: String e5 = new String(e.getBytes("utf-8"),"big5");
: String e8 = new String(e5.getBytes("big5"),"utf-8");
問題在這裡
你這個寫法
e5是把一個utf-8的字串強制認為它是big5
所以不屬於big5編碼的符號就變成 '?'(0x3f) 了
e8同樣 是把一個big5的字串強制認為它是utf-8 同樣會出問題
不認識的字會變成 U+FFFD (utf-8: 0xef 0xbf 0xbd, "REPLACEMENT CHARACTER")
這其實根本不是在做轉碼...XD
: System.out.println(e);
: byte y[]=e.getBytes("utf-8");
: for (int i=0; i<y.length; i++) {
: System.out.printf("%x ", y[i]);
: }
: System.out.println();
: System.out.println(e5);
: byte x[]=e5.getBytes("big5");
: for (int i=0; i<x.length; i++) {
: System.out.printf("%x ", x[i]);
: }
: System.out.println();
: System.out.println(e8);
: byte z[]=e8.getBytes("utf-8");
: for (int i=0; i<z.length; i++) {
: System.out.printf("%x ", z[i]);
: }
: }
: }
: 結果原始的文字轉成 big5 後轉不回 utf-8
: 輸出結果
: utf-8
: 中文轉碼測試
: e4 b8 ad e6 96 87 e8 bd 89 e7 a2 bc e6 b8 ac e8 a9 a6
: ????????????
: e4 b8 ad e6 3f e8 bd 3f a2 bc e6 b8 ac e8 a9 a6
: ????????????
: e4 b8 ad ef bf bd 3f ef bf bd 3f ef bf bd ef bf bd e6 b8 ac e8 a9 a6
: 不知道有人遇到過嗎?
其實轉碼這種事只要在輸出入時做轉碼就好了
內部就讓它用自己的unicode做事就好
個人以為這正是為什麼java堅持內部一定是unicode的原因...
C/C++這方面就不太好做 你自己必須要清楚你現在存的是什麼編碼
有沒有什麼奇怪的issue要注意 什麼時候要轉 什麼時候直接用就好等等
--
[LPH] Oops, your OOP's a problem? 說:
你現在還是看不到狗?
************* 說:
看得到 只是 他們不會跑 就一直呆呆在那邊 一直在起點
[LPH] Oops, your OOP's a problem? 說:
你要按"ㄅㄧㄤˋ"它們才會跑啊@@"
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.30.84
推
12/07 15:01, , 1F
12/07 15:01, 1F
推
12/07 15:48, , 2F
12/07 15:48, 2F
推
12/07 15:50, , 3F
12/07 15:50, 3F
推
12/07 16:38, , 4F
12/07 16:38, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):