short数组保存unicode编码和unicode编码转换成shrot数组

  1. 将short数组转换成中文字符串
 short[] ausContent32 ={20320, 24597, 26159, 49, 49, 49, 20010, 20667, 23376, 21679,0} 
 public String getAusContent32() {
        String str = "";
        if (ausContent32.length > 0) {
            for (int x = 0; x < ausContent32.length; x++) {
                if (ausContent32[x] == 0) {
                    break;
                } else {
                    short s = ausContent32[x];
                    String st = Integer.toHexString(s); //转还成十六进制  将十六进制转换成字符
                    String stt = "\\u" + st.trim();
                    str += decodeUnicode(stt).trim();
                }
            }
        }
        return str;
    }
 public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }
  1. 将中文字符串,转换成short数组,进行保存
  public void setAusContent32(String ausContent32) {
        String ausContent = gbEncoding(ausContent32.trim());
        Log.e("TSubtitle", "setAusContent32: "+ausContent);
        String[] str = ausContent.split("\\\\u");
        String[] str1 = new String[str.length];
        int f = 0;
        for (int x = 0; x < str.length; x++) {
            if (!str[x].trim().isEmpty()) {
                str1[f++] = str[x];
            }
        }
        String[] str11 = new String[f];
        for (int x=0;x<f;x++){
            str11[x] =str1[x];
        }
        short[] s = new short[32];
        if (str11.length > 0) {
            for (int x = 0; x <= str11.length; x++) {
               if (x==str11.length){
                   s[x]=0;
               }else {
                   Log.e("TSubtitle", "setAusContent32:: x:" + x + "    str11:" + str11[x]);
                   int t = Integer.parseInt(str11[x].trim(), 16);
                   s[x] = (short) t;
               }
            }
        }

        this.ausContent32 = s;
    }
  public static String gbEncoding(final String gbString) {   
        char[] utfBytes = gbString.toCharArray();   
        String unicodeBytes = "";
        for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
            String hexB = Integer.toHexString(utfBytes[byteIndex]);   //转换为16进制整型字符串
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        System.out.println("unicodeBytes is: " + unicodeBytes);
        return unicodeBytes;
    }

猜你喜欢

转载自blog.csdn.net/viking_xhg/article/details/80681192