• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Parse binary data

Hi,

I'm new here and i have a little question about an app that i am developing.

I'm new at mobile development and i have concerns like eficiency, that are very important.

I have an application that receives data in binary form through bluetooth. I read the data using bluetoothsocket to an byte[]. But i must parse all messages, because they must have an given format and they are in binary.

My solution was to convert byte[] to a string and then split the string and parse all received messages.

I don't know if this is the best way to do it and therefore i am asking to help me in this issue.

An example of the data to parse:
0000000010000001

i should know that the first 8 zeros are the header and 10000001 the real data.

My idea was to create a string that represents -> 0000000010000001
and then split the whole string in one byte and check the value, like:

string1 had 00000000
string2 had 10000001

i know that 8 zeros are the header, therefore string2 has the representation of the data.

regards.
 
You are correct in assuming that as the best approach. Somthing like this would suffice:

Code:
//byteArray is the array of bytes recieved via BT
byte[] header = Arrays.copyOfRange(byteArray, 0, endIdx); //endIdx is the last byte in the aray to be included in the header
byte[] body = Arrays.copyOfRange(byteArray, endIdx+1, header.length-1);
 
String headerStr = new String(header);
String bodyStr = new String(body);
String header = byteStr.substring
 
Back
Top Bottom