Android,Integer.parseInt()不起作用

时间:2014-03-28 18:14:05

标签: android

抱歉我的英语不好..所以..我有这段代码:

    void inputthread() {
final Handler handler = new Handler(); 
final int misurazione[]={0};
final byte delimiter = 65; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
  public void run() {
 while(!Thread.currentThread().isInterrupted() && !stopWorker) 
 {
  try 
  {
    int bytesAvailable = mmInputStream.available();            
    if(bytesAvailable > 0) 
    { 
        byte[] packetBytes = new byte[bytesAvailable];
        mmInputStream.read(packetBytes);
        for(int i=0;i<bytesAvailable;i++) 
        {
            byte b = packetBytes[i];
            if(b == delimiter)
            {
              byte[] encodedBytes = new byte[readBufferPosition];
              System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
              String data = new String(encodedBytes, "US-ASCII");   
              readBufferPosition = 0;
                try {
                      misurazione[0] = Integer.parseInt(data);
                      handler.post(new Runnable() 
                          {
                              public void run()
                              {
                                txtaggiorna.setText(misurazione[0]);
                              }
                          }); 
                    } catch(NumberFormatException nfe) {}
              }
                else {
                  readBuffer[readBufferPosition++] = b;
                }
       }
     }
  }catch (IOException ex) 
      {
        stopWorker = true;
      }
}
  }
});

workerThread.start();
  }

Android从仅包含数字字符串的arduino接收字节,我想将其转换为整数,但是Integer.parseInt(数据)不起作用,我不知道为什么......

1 个答案:

答案 0 :(得分:0)

 String data = new String(encodedBytes.toString(), "US-ASCII");