2013-02-25 74 views
2
public static Runnable reader() throws IOException { 
    Log.e("Communication", "reader"); 
    din = new DataInputStream(sock.getInputStream()); 
    brdr = new BufferedReader(new InputStreamReader(din), 300); 
    boolean done = false; 
    while (!done) { 
     try { 
      char[] buffer = new char[200]; 
      int anzahlZeichen = brdr.read(buffer, 0, 200); 
      String nachricht = new String(buffer, 0, anzahlZeichen); 
      byte[] in = nachricht.getBytes("ISO-8859-1"); 
      for (int counter = 0; counter < nachricht.length(); counter++) { 
       System.out.println(in); 
      } 
      if (nachricht != null) 
       answer(); 
      System.out.println(nachricht); 

     } catch (IOException ioe) { 
      done = true; 
     } 
    } 

    return null; 
} 

我想字符串nachricht轉換爲字節[] in,但我不明白這一點。任何人都可以幫忙嗎?我只接受數字,沒有文字或字母。另一種方法也是受歡迎的。所有我在System.out.println(nachricht)是七次[[email protected],但我應該得到01 02 03 04 05 06 07遇到麻煩字符串轉換爲byte []

+1

您不應該在同一個項目中使用來自不同語言的變量名;保持一致,這將使您的代碼更易於閱讀。 – 2013-02-25 13:26:40

+0

thx G.Bach,我會這樣做 – Ekonion 2013-02-25 13:27:29

回答

2

你的問題是線System.out.println(in)

應該System.out.println(in[counter]);

+0

omg ...尷尬^^ thx很多將在11分鐘內接受 – Ekonion 2013-02-25 13:18:38

3

[[email protected] 

表明你打印一個字節數組([)()。

Java數組沒有有用的toString()實現。以上內容有助於理解,因爲您將來也會這樣做。其他原始類型存在明顯的助記符。

+0

有用的知識,thx – Ekonion 2013-02-25 13:33:14