Java 的Big5-E與UTF-8的轉碼

說眞的,只為了解決一個對應碼錯誤的問題!
public class ChartsetTrans {

public static void main(String[] args) {
// System.out.println( getEncodingString(“眞", “UTF-8″, “MS950″) );
String s = getEncodingString(“", “MS950″, “UTF-8″);
System.out.println( s + “眞".equals(s) );

try {
System.out.println( getEncodingString(“嫺", “UTF-8″, “MS950″, true) );
String sbig5 = new String(new byte[]{(byte)0xF9, (byte)0xDC}, “MS950″);
System.out.println(sbig5);
s = getEncodingString(sbig5, “MS950″, “UTF-8″, true);
System.out.println( s + “嫺".equals(s) );
} catch (java.io.UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}

private static final byte [][][] MS950_UTF8 = new byte[][][] {
// [0] = MS950, [1] = UTF-8
new byte[][]{new byte[]{-105, 84}, new byte[]{-25, -100, -98} } // 眞 MS950:(byte)0x97 (byte)0x54 // UTF-8: (byte)0xE7, (byte)0x9C, (byte)0x9E
};
public static String getEncodingString(String str, String srcEnc, String targetEnc) {
return getEncodingString(str, srcEnc, targetEnc, false);
}
public static String getEncodingString(String str, String srcEnc, String targetEnc, boolean debug) {
try {
byte[] bytes = str.getBytes(srcEnc);
if (debug){
for (int i=0;i<bytes.length; i++){
System.out.printf(“%02X “, bytes[i]);
}
System.out.println(" (" + srcEnc + “)");
}
if (“MS950″.equalsIgnoreCase(srcEnc) && “UTF-8″.equalsIgnoreCase(targetEnc)){
StringBuffer sb = new StringBuffer();
int j = 0;
for (int i=0;i<bytes.length; i+=2 ){
for (byte [][] bt : MS950_UTF8){
if (bytes.length >= 2 && bytes[i] == bt[0][0] && bytes[i+1] == bt[0][1] ){ // (byte)0x97 (byte)0x54
if (i>j){
sb.append( getEncodingString(java.util.Arrays.copyOfRange(bytes, j, i), srcEnc, targetEnc) );
}
// sb.append( new String(new byte[]{-25, -100, -98}, targetEnc) ); // (byte)0xE7, (byte)0x9C, (byte)0x9E
sb.append( new String( bt[1], targetEnc) ); // (byte)0xE7, (byte)0x9C, (byte)0x9E
j = i+2;
break;
}
}
}
if (bytes.length>j){
sb.append( getEncodingString( java.util.Arrays.copyOfRange(bytes, j, bytes.length), srcEnc, targetEnc) );
}
return sb.toString();
}else{
return getEncodingString( bytes, srcEnc, targetEnc);
}
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

private static final String getEncodingString(byte [] bytes, String srcEnc, String targetEnc){
String rtn = null;
try{
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.ByteArrayInputStream( bytes ), srcEnc));
java.io.ByteArrayOutputStream boas = new java.io.ByteArrayOutputStream();
java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(boas, targetEnc);
osw.write(br.readLine());
osw.flush();
rtn = boas.toString(targetEnc);
br.close();
boas.close();
osw.close();
} catch (java.io.UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (java.io.IOException ex) {
ex.printStackTrace();
}
return rtn;
}
}

廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s