返回列表 發帖

TQC209 - 文字編碼的轉換

本帖最後由 tonyh 於 2013-7-20 17:02 編輯
  1. import java.io.UnsupportedEncodingException;
  2. public class tqc209
  3. {
  4.     public static void main(String args[]) throws UnsupportedEncodingException
  5.     {
  6.         String str="JAVA程式";
  7.         String toHex=toHex(str);
  8.         System.out.println("Unicode 碼 16 進位\t"+toHex);
  9.         String toBig5=new String(str.getBytes("Big5"),"ISO8859_1");
  10.         String inHex=toHex(toBig5);
  11.         System.out.println("Big5 碼 16 進位\t\t"+inHex);
  12.         String toStr=new String(toBig5.getBytes("ISO8859_1"),"Big5");
  13.         System.out.println("原字串\t\t\t"+toStr);
  14.     }

  15.     public static String toHex(String str)  //自訂函式, 回傳的變數型態為String
  16.     {
  17.         String res="";
  18.         char tmp;
  19.         for(int i=0; i<str.length(); i++)
  20.         {
  21.              tmp=str.charAt(i);
  22.              String t="0000"+Integer.toHexString(tmp);
  23.              res+=t.substring(t.length()-4)+" ";
  24.         }
  25.         return res;
  26.     }
  27. }
複製代碼

返回列表