1. 程式人生 > >java字元與對應Ascii碼互轉

java字元與對應Ascii碼互轉



圖題

程式碼如下:

[java]  view plain  copy
  1. package main;  
  2.   
  3. /** 
  4.  * Java中將一個字元與對應Ascii碼互轉 
  5.  * 1 byte = 8bit 可以表示 0-127
     
  6.  */  
  7. public class GetCharAscii {  
  8.   
  9.     /*0-9對應Ascii 48-57 
  10.      *A-Z 65-90 
  11.      *a-z 97-122 
  12.      *第33~126號(共94個)是字元,其中第48~57號為0~9十個阿拉伯數字
     
  13.      */  
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub  
  16.   
  17.         System.out.println(charToByteAscii('9'
    ));  
  18.         System.out.println(byteAsciiToChar(57));  
  19.         System.out.println(SumStrAscii("19"));  
  20.         System.out.println(SumStrAscii("一"));  
  21.     }  
  22.   
  23.     /** 
  24.      * 方法一:將char 強制轉換為byte 
  25.      * @param ch 
  26.      * @return 
  27.      */  
  28.     public static byte charToByteAscii(char ch){  
  29.         byte byteAscii = (byte)ch;  
  30.           
  31.         return byteAscii;  
  32.     }  
  33.     /** 
  34.      * 方法二:將char直接轉化為int,其值就是字元的ascii 
  35.      * @param ch 
  36.      * @return 
  37.      */  
  38.     public static byte charToByteAscii2(char ch){  
  39.         byte byteAscii = (byte)ch;  
  40.           
  41.         return byteAscii;  
  42.     }  
  43.     /** 
  44.      * 同理,ascii轉換為char 直接int強制轉換為char 
  45.      * @param ascii 
  46.      * @return 
  47.      */  
  48.     public static char byteAsciiToChar(int ascii){  
  49.         char ch = (char)ascii;  
  50.         return ch;  
  51.     }  
  52.     /** 
  53.      * 求出字串的ASCII值和 
  54.      * 注意,如果有中文的話,會把一個漢字用兩個byte來表示,其值是負數 
  55.      */  
  56.     public static int SumStrAscii(String str){  
  57.         byte[] bytestr = str.getBytes();  
  58.         int sum = 0;  
  59.         for(int i=0;i<bytestr.length;i++){  
  60.             sum += bytestr[i];  
  61.         }  
  62.         return sum;  
  63.     }  
  64. }  

圖題

程式碼如下:

[java]  view plain  copy
  1. package main;  
  2.   
  3. /** 
  4.  * Java中將一個字元與對應Ascii碼互轉 
  5.  * 1 byte = 8bit 可以表示 0-127 
  6.  */  
  7. public class GetCharAscii {  
  8.   
  9.     /*0-9對應Ascii 48-57 
  10.      *A-Z 65-90 
  11.      *a-z 97-122 
  12.      *第33~126號(共94個)是字元,其中第48~57號為0~9十個阿拉伯數字 
  13.      */  
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub  
  16.   
  17.         System.out.println(charToByteAscii('9'));  
  18.         System.out.println(byteAsciiToChar(57));  
  19.         System.out.println(SumStrAscii("19"));  
  20.         System.out.println(SumStrAscii("一"));  
  21.     }  
  22.   
  23.     /** 
  24.      * 方法一:將char 強制轉換為byte 
  25.      * @param ch 
  26.      * @return 
  27.      */  
  28.     public static byte charToByteAscii(char ch){  
  29.         byte byteAscii = (byte)ch;  
  30.           
  31.         return byteAscii;  
  32.     }  
  33.     /** 
  34.      * 方法二:將char直接轉化為int,其值就是字元的ascii 
  35.      * @param ch 
  36.      * @return 
  37.      */  
  38.     public static byte charToByteAscii2(char ch){  
  39.         byte byteAscii = (byte)ch;  
  40.           
  41.         return byteAscii;  
  42.     }  
  43.     /** 
  44.      * 同理,ascii轉換為char 直接int強制轉換為char 
  45.      * @param ascii 
  46.      * @return 
  47.      */  
  48.     public static char byteAsciiToChar(int ascii){  
  49.         char ch = (char)ascii;  
  50.         return ch;  
  51.     }  
  52.     /** 
  53.      * 求出字串的ASCII值和 
  54.      * 注意,如果有中文的話,會把一個漢字用兩個byte來表示,其值是負數 
  55.      */  
  56.     public static int SumStrAscii(String str){  
  57.         byte[] bytestr = str.getBytes();  
  58.         int sum = 0;  
  59.         for(int i=0;i<bytestr.length;i++){  
  60.             sum += bytestr[i];  
  61.         }  
  62.         return sum;  
  63.     }  
  64. }