0

I'm trying to send Arduino ADC data to android tablet using USB serial Communication. I'm using Serial.println() at arduino side. My Issue is that I'm not able to Decode the data received at the android end.
For Eg. Suppose I send Serial.println(768) from arduino, I check my android receive buffer and it Shows (55,54,56,13,10).

How can i decode this data back to 768 value?

  • Possible duplicate of [How to convert byte array to string and vice versa?](https://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa) – Piglet Oct 04 '19 at 09:26

2 Answers2

1

Looking into an ASCII table you'll find that

55,54,56,13,10

represents

"768\n\r"

Most programming languages provide means for conversion between byte values and characters/strings with their string libraries. So you don't have to implement the decoding yourself.

Refer to https://howtodoinjava.com/array/convert-byte-array-string-vice-versa/

or UTF-8 byte[] to String

or anything else you find online for "byte to string Android"

Piglet
  • 18,719
  • 2
  • 16
  • 30
  • Thanks for your reply. The link you provided does help me solving my issue. I had already figured out the decoding part after posting my question.Actually the .getbytes method also puts new CR & LN in the string but I wanted to omit those bytes and only interested in number part – Sumit Mourya Oct 04 '19 at 10:20
0

String rawdata = " " ; String finaldata = " "; UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read. @Override public void onReceivedData(byte[] arg0) {

        byte[] buffer = arg0;
        for (i =0;i <=(buffer.length-1);i++) {
         if(buffer[i]!= 13) {
             if(buffer[i]== 10){
                 finaldata = rawdata;
                 rawdata = "";
             }else {
                 chdata = (char) buffer[i];
                 rawdata += chdata;
             }
         }

      }

            data = Integer.parseInt(finaldata);

}