當前位置:
首頁 > 知識 > short數組保存unicode編碼和unicode編碼轉換成shrot數組

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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

  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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
}

short數組保存unicode編碼和unicode編碼轉換成shrot數組

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

C井處理html 標籤一些正則表達式 整理收集
listview多種條目展示案例

TAG:程序員小新人學習 |