國際化工具類Ascii2NativeUtils 與Native2AsciiUtils


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Ascii2NativeUtils {
     /** 
          * prefix of ascii string of native character 
          */ 
         private static String PREFIX = "//u";
   
     /** 
          * Ascii to native string. It's same as execut native2ascii.exe -reverse. 
          *  
          * @param str 
          *            ascii string 
          * @return native string 
          */ 
         public static String ascii2Native(String str) {  
             StringBuilder sb = new StringBuilder();  
             int begin = 0;  
             int index = str.indexOf(PREFIX);  
             while (index != -1) {  
                 sb.append(str.substring(begin, index));  
                 sb.append(ascii2Char(str.substring(index, index + 6)));  
                 begin = index + 6;  
                 index = str.indexOf(PREFIX, begin);  
             }  
             sb.append(str.substring(begin));  
             return sb.toString();  
         }  
      
         /** 
          * Ascii to native character. 
          *  
          * @param str 
          *            ascii string 
          * @return native character 
          */ 
         private static char ascii2Char(String str) {  
             if (str.length() != 6) {  
                 throw new IllegalArgumentException(  
                         "Ascii string of a native character must be 6 character.");  
             }  
             if (!PREFIX.equals(str.substring(0, 2))) {  
                 throw new IllegalArgumentException(  
                         "Ascii string of a native character must start with /"//u/".");  
             }  
             String tmp = str.substring(2, 4);  
             int code = Integer.parseInt(tmp, 16) << 8;  
             tmp = str.substring(4, 6);  
             code += Integer.parseInt(tmp, 16);  
             return (char) code;  
         }  
        
         public static void main(String[] args) {
        File file = new File("E://eclipse//PDE-eclipse//eclipse//workspace//" +
                "org.trustie.loong.pde.ds.ui.nl_zh//src//org//trustie//loong//pde//ds//ui//messages_zh.properties");
        BufferedReader reader = null;
        try {
            System.out.println("以行爲單位讀取文件內容,一次讀一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次讀入一行,直到讀入null爲文件結束
            while ((tempString = reader.readLine()) != null) {
                // 顯示行號
//                System.out.println("line " + line + ": " + tempString);
//                line++;
                System.out.println(ascii2Native(tempString));
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

//        String ascii = "";
//        System.out.println(ascii2Native(ascii));

    }

}

 

 

-----------------------------------------------------------------------------------

public class Native2AsciiUtils {
   
   
     /**
     * prefix of ascii string of native character
     */
    private static String PREFIX = "//u";

    /**
     * Native to ascii string. It's same as execut native2ascii.exe.
     *
     * @param str
     *            native string
     * @return ascii string
     */
    public static String native2Ascii(String str) {
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            sb.append(char2Ascii(chars[i]));
        }
        return sb.toString();
    }  
   
     /**
     * Native character to ascii string.
     *
     * @param c
     *            native character
     * @return ascii string
     */
    private static String char2Ascii(char c) {
        if (c > 255) {
            StringBuilder sb = new StringBuilder();
            sb.append(PREFIX);
            int code = (c >> 8);
            String tmp = Integer.toHexString(code);
            if (tmp.length() == 1) {
                sb.append("0");
            }
            sb.append(tmp);
            code = (c & 0xFF);
            tmp = Integer.toHexString(code);
            if (tmp.length() == 1) {
                sb.append("0");
            }
            sb.append(tmp);
            return sb.toString();
        } else {
            return Character.toString(c);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String ss = "哈哈";
        System.out.println(native2Ascii(ss));
    }
   
   

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章