1. Are you ready for the Galaxy S20? Here is everything we know so far!

Noob problem: how to parse binary data into structs

Discussion in 'Android Development' started by hackster84, May 15, 2014.

  1. hackster84

    hackster84 Lurker
    Thread Starter

    Hi,

    I am a noob in the Java/Android development world. I primarily do hardware and firmware design, but a new product my company is developing has the need for an Android app and since I have the most Android programming experience (which is extremely limited) that leaves the task to me. I need a little guidance figuring out how to do a few things so that I can get this app up and running. I already have a simple GUI to display some stuff, but what I need is a way to handle data.

    I am trying to receive a 148 byte stream of data over a serial connection, parse it into a structure and display certain values from that structure. The data is packed in a struct that is sent from a micro controller and due to that it has a very specific layout (i.e. the first 8 bytes mean this, the next 16 bytes are this... ). In C, I can just declare a struct with those values and use the #pragma pack function to eliminate any extra spacing the compiler would otherwise inject for alignment purposes, receive the data and do a memcpy into the struct to write the data. It's not the safest or cleanest way to do it, but it takes very little time and if you do CRC tests to make sure the data received is valid it works like a charm.

    Now to the root of the problem: How do I declare structs in Java? Is there a way to pack them tightly like you can using C? Once I've received the binary data, is there a way to parse it into the structure easily? These are the issues we hardware programmers face when dealing with Java...

    Any help would be greatly appreciated, thank you.

    -Chris
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Ranandar

    Ranandar Lurker

    Simple Data Structure Example
    I hope this help or at least gives you some direction on what to search for.

    Create a class...
    Code (Text):
    1.  
    2.     public class my_structure    
    3.     {
    4.             String name;        
    5.             short age;          
    6.             short height;              
    7.             short department;                      
    8.             short extension;                
    9.  
    10.             // Constructor loads default values
    11.             public my_structure() {
    12.                    this.name = "New Name ";
    13.                    this.age= 0;
    14.                    this.height= 0;
    15.                    this.department= 0;
    16.                    this.extension= 0;
    17.             }
    18.     }
    19.  
    Create new object from above class...
    Code (Text):
    1.  
    2.    private my_structure mystructure = new my_structure();
    3.  
    Access class info...
    Code (Text):
    1.  
    2.    mystructure.name = "Joe Smith";
    3.    mystructure.age = 28;
    4.    mystructure.height = 70;
    5.  
    In C I use a memory dump as you describe, but that will only work if the two devices share exactly the same architecture. In the case of Android you will probably want things to work across various platforms so you will have to pars your data using substring, parseInt, etc.

    You can also place functions inside of the class that set and retrieve data or make calculations based on the data.
     
  3. hackster84

    hackster84 Lurker
    Thread Starter

    Thank you for the example, this should help a lot. I have been reading a lot and I did come across some references to being able to directly use c code in android programs. The references were based on old android operating systems though (like 1.5 or something along those lines). Would it be easier to use modified c code that's already written to deal with the data or should this all be done in java? The references were to the NDK and looking at the current version of the android SDK revealed that there is a package installed called NDK - is that the same native development kit that worked for android 1.5 and if so does it still work for newer versions of the operating system?
     
Loading...

Share This Page

Loading...