国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

手機上通用的UTF8轉換程序

2019-11-18 16:06:11
字體:
來源:轉載
供稿:網友

最近發現java的UTF轉換函數有BUG,在某些手機上(如K700)會出現亂碼,與是上網查詢了一下,在國外論壇上找到了一個UTF8轉換函數,在我們公司所有測試機上都能正常轉換,覺得效果還可以

 

    PRivate final String readUnicodeFileUTF8(String filename) {
        StringBuffer sb = new StringBuffer(256);
        try {
            int[] surrogatePair = new int[2];
            InputStream is = this.getClass().getResourceAsStream(filename);

            int val = 0;
            int unicharCount = 0;
            while ((val = readNextCharFromStreamUTF8(is))!=-1) {
                unicharCount++;
                if (val <= 0xFFFF) {
                    // if first value is the Byte Order Mark (BOM), do not add
                    if (! (unicharCount == 1 && val == 0xFEFF)) {
                        sb.append((char)val);
                    }
                } else {
                    supplementCodePointToSurrogatePair(val, surrogatePair);
                    sb.append((char)surrogatePair[0]);
                    sb.append((char)surrogatePair[1]);
                }
            }
            is.close();
        } catch (Exception e) {};

        return new String(sb);
    }
  
    private final static int readNextCharFromStreamUTF8(InputStream is) {
        int c = -1;
        if (is==null) return c;
        boolean complete = false;
      
        try {
            int byteVal;
            int eXPecting=0;
            int composedVal=0;
          
            while (!complete && (byteVal = is.read()) != -1) {
                if (expecting > 0 && (byteVal & 0xC0) == 0x80) {  /* 10xxxxxx */
                    expecting--;
                    composedVal = composedVal ((byteVal & 0x3F) << (expecting*6));
                    if (expecting == 0) {
                        c = composedVal;
                        complete = true;
                        //System.out.println("appending: U+" + Integer.toHexString(composedVal) );
                    }
                } else {
                    composedVal = 0;
                    expecting = 0;
                    if ((byteVal & 0x80) == 0) {    /* 0xxxxxxx */
                        // one byte character, no extending byte expected
                        c = byteVal;
                        complete = true;
                        //System.out.println("appending: U+" + Integer.toHexString(byteVal) );
                    } else if ((byteVal & 0xE0) == 0xC0) {  /* 110xxxxx */
                        expecting = 1;  // expecting 1 extending byte
                        composedVal = ((byteVal & 0x1F) << 6);
                    } else if ((byteVal & 0xF0) == 0xE0) {  /* 1110xxxx */
                        expecting = 2;  // expecting 2 extending bytes
                        composedVal = ((byteVal & 0x0F) << 12);
                    } else if ((byteVal & 0xF8) == 0xF0) {  /* 11110xxx */
                        expecting = 3;  // expecting 3 extending bytes
                        composedVal = ((byteVal & 0x07) << 18);
                    } else {
                        // non conformant utf-8, ignore or catch error
                    }
                }
            }
          
        } catch (Exception e) {
            System.out.println(e.toString());
        }
      
        return c;
    }


    private final static void supplementCodePointToSurrogatePair(int codePoint, int[] surrogatePair) {
        int high4 = ((codePoint >> 16) & 0x1F) - 1;
        int mid6 = ((codePoint >> 10) & 0x3F);
        int low10 = codePoint & 0x3FF;

        surrogatePair[0] = (0xD800 (high4 << 6) (mid6));
        surrogatePair[1] = (0xDC00 (low10));
    }

 

(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石城县| 呼玛县| 贵南县| 元氏县| 安塞县| 黄石市| 辉县市| 澄迈县| 遂平县| 湖州市| 麟游县| 本溪市| 宁乡县| 正镶白旗| 甘泉县| 成都市| 隆化县| 东乌珠穆沁旗| 洛川县| 习水县| 若尔盖县| 綦江县| 怀化市| 松溪县| 肥乡县| 南雄市| 广西| 瓮安县| 沾益县| 阿克陶县| 尉犁县| 凤城市| 阳信县| 藁城市| 峡江县| 镇赉县| 宁晋县| 雷山县| 武城县| 衡阳县| 南城县|