2013-02-25 73 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