简体   繁体   中英

Converting a String from Byte array to int

I've been googling all night for this problem since it is my first time to use arduino and android. My question is, how can I convert the variable data *String to int? I've been getting NumberFormatException whenever I do: int pulse = Integer.ParseInt(data);

My objective here is to be able to get the data coming from the arduino and have it as an integer, for me to be able to compare it.

ADDITIONAL: The variable "data" is a pulse rate. I need to have it converted to int so I can compare the value if the pulse rate is still normal or not. After hours of searching I found out that what I am trying to convert to int is not purely a string since it came from the arduino, now my problem is how can I make the variable "data" an integer.

This is my code:

public void run()
        {                
            while(!Thread.currentThread().isInterrupted() && !stopWorker)
            {
                try 
                {
                    final int bytesAvailable = mmInputStream.available();                        
                    if(bytesAvailable > 0)
                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.read(packetBytes);
                        for(i=0;i<bytesAvailable;i++)
                        {
                            byte b = packetBytes[i];
                            if(b == delimiter)
                            {
                                final byte[] encodedBytes = new byte[readBufferPosition];
                                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                final String data = new String(encodedBytes, "US-ASCII");
                                readBufferPosition = 0;

                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {   

                                        Intent i = new Intent(Bluetooth.this, Home.class);
                                        i.putExtra("theBPM",data);
                                        startActivity(i);
                                    }
                                });
                            }
                            else
                            {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }
                } 
                catch (IOException ex) 
                {
                    stopWorker = true;
                }
            }
        }
    });

    workerThread.start();

Thank you.

We assume that the problem is String to int and you already stepped in debug mode or make some Toast message that you are getting the string representation as expected.

You can try the following option:

Try printing/logging/making a toast to see what is in data . This can happen only when data has characters other than digits or may be you have a double(decimal) value in data . For that you might need to use Double.parseDouble(data) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM