1. Download our Official Android App: Forums for Android!

Apps Parse binary data

Discussion in 'Android Development' started by captainTsubasa, Sep 22, 2011.

  1. captainTsubasa

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Sep 22, 2011

    Sep 22, 2011
    2
    0
    5
    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.
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    You are correct in assuming that as the best approach. Somthing like this would suffice:

    Code (Text):
    1.  
    2. //byteArray is the array of bytes recieved via BT
    3. byte[] header = Arrays.copyOfRange(byteArray, 0, endIdx); //endIdx is the last byte in the aray to be included in the header
    4. byte[] body = Arrays.copyOfRange(byteArray, endIdx+1, header.length-1);
    5.  
    6. String headerStr = new String(header);
    7. String bodyStr = new String(body);
    8.  
    String header = byteStr.substring
     

Share This Page

Loading...