1. 程式人生 > >Java將二進位制轉為ascii碼

Java將二進位制轉為ascii碼

public class ToAscii {  
  
    public static void main(String[] args) throws IOException {  
        BufferedReader br = new BufferedReader(new InputStreamReader(  
                new FileInputStream("binary.txt")));  
        String[] ss=null;//用來儲存第一行的各項  
        for(;;) {  
            String str = br.readLine();  
            //如果讀到檔案最後一行之後,則退出迴圈  
            if(str==null)  
                break;  
            ss=str.split(" ");//每一行的各項用空格分開  
            for (String s : ss) {  
                int numasc = Integer.parseInt(s, 2);  
                System.out.print((char)numasc);//強制轉換成char型別,並輸出  
            }  
        }  
        br.close();  
    }  
}