Java 中文與ASCII碼轉換


import java.io.UnsupportedEncodingException;
public class T {
 public static void main(String[] args) throws UnsupportedEncodingException {
  t1();//ASCII轉換為字符串

  t2();//字符串轉換為ASCII碼

 }
 public static void t1(){//ASCII轉換為字符串

  String s="22307 35806 24555 20048";//ASCII碼

  String[]chars=s.split(" ");
  System.out.println("ASCII 漢字 \n----------------------");
        for(int i=0;i<chars.length;i++){ 
            System.out.println(chars[i]+" "+(char)Integer.parseInt(chars[i]));
        } 
 }
 public static void t2(){//字符串轉換為ASCII碼

  String s="新年快樂!";//字符串

  char[]chars=s.toCharArray(); //把字符中轉換為字符數組 

  System.out.println("\n\n漢字 ASCII\n----------------------");
  for(int i=0;i<chars.length;i++){//輸出結果

         System.out.println(" "+chars[i]+" "+(int)chars[i]);
        }
 }
}

  轉載:http://blog.chinaunix.net/uid-20447986-id-1945654.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM