Android 台灣中文網

標題: 取讀 BIG5 碼檔案 轉換成 UTF-16LE 文字格式輸出 [打印本頁]

作者: jianrupan    時間: 2020-5-11 17:17
標題: 取讀 BIG5 碼檔案 轉換成 UTF-16LE 文字格式輸出
本文說明將 BIG 5 格式的文件檔,讀取後為 Byte 形式,如何轉換為 UTF-16LE 文字格式輸出。

執行方式程式段說明:
        // 讀取檔案
        BufferedInputStream bReader = new BufferedInputStream(new FileInputStream(sFile));
        // 讀取 tCnt Byte 放入 Buf 0 開始位置
        rCnt = bReader.read(Buf, 0, tCnt);
        // Byte BIG5 轉為 UTF-16LE
        sName = StringUtil.MTByte2Str(Buf, "BIG5", "UTF-16LE");
            
        // Byte 文字轉換(exp: BIG5 轉為 UTF-16LE)
        public static String MTByte2Str(byte[] tBuf, String sType, String tType) {
            String retStr = null;
            try {
                // Byte 轉為目前 文字 Type String (BIG5)
                retStr = new String(tBuf, sType);
                // String 轉為目前 指定文字 Type Byte (UTF-16LE)
                byte y[]=retStr.getBytes(tType);
                int gCnt = 0;
                for (gCnt=0; gCnt<tBuf.length; gCnt++) {
                    if(0 == y[gCnt])
                            break;
                }
                // Byte 轉為目前 指定文字 Type String (UTF-16LE)
                retStr = new String(y, 0, gCnt, tType);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return retStr;
        }







歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1