Analysis of data in the structure

I am working on a project where I need to communicate with a device via TCP / IP protocol. The device sends a large amount of data, which somehow I want to parse into some objects / structures.

Datapackage example (in TCP buffer []):

[64] [1] [78] [244] [77] [189] [249] [149] hcurrent
[64] [1] [78] [247] [89] [95] [104] [85] htarget
[0] [0] [0] [0] [0] [0] [0] [0] qcurrent
[188] [220] [97] [3] [66] [62] [0] [0] kcurrent
[66] [0] [102] [103] [66] [99][153] [154] mcurrent
[253] [191] [246] [74] [170] [216] [242] [29] fmode
[102] [191] [246] [74] [178] [44] [92] [72] tmil
[137] mode

Now this package package is identified as:

 double hcurrent
 double htarget
 double qcurrent
 float kcurrent
 float mcurrent
 float fmode
 float tmil
 unsigned char mode

My idea was that somehow I could analyze the data directly in a structure that had the same structure as above. Of course, some key values ​​need to be defined in order to determine what type of data it has.

How can I do that?

Since I am encoding an iOS device, it must be objective-C or C (++).

EDIT (method tested to copy each part of the datagram into a structure): Small Java implementation where I try to read the first 4 bytes [0] [0] [1] [5]:

byte[] read = new byte[4]; 
int length = 0;
while (length < read.length) {
    len = iStream.read(read, len, read.length);
}
int ByteLength = (int)unsignedIntToLong(read);
ByteLength = ByteLength-5;
state = 1; // Continue and work with next data.

:

public long unsignedIntToLong(byte[] b) 
{
    long l = 0;
    l |= b[0] & 0xFF;
    l <<= 8;
    l |= b[1] & 0xFF;
    l <<= 8;
    l |= b[2] & 0xFF;
    l <<= 8;
    l |= b[3] & 0xFF;
    return l;
}

, 4 , , - , 465. , .

+3
2

, , , ,

, , [], , . , , , memcpy [] , [].

, , . MSVC # pragma pack . .

EDIT: , , , . :

// We want to copy raw data to this structure
// but the short will cause it to be unaligned
struct _parsed_structure
{
    int a;
    int b;
    short c;
    int d;
} parsed_structure;

template<typename T>
void read_and_update_offset (int & offset, char * buffer, T & var)
{
    T * pInt = (T*)(buffer + offset);
    var = *pInt;
    offset += sizeof(T);
};

int _tmain(int argc, _TCHAR* argv[])
{
    // Here a buffer which we know contains ints and shorts, we could just cast it to our structure
    // but this will cause errors because the structure will not be aligned properly.
    char buffer[] = { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0 };

    // Read the first int from the buffer into the structure
    int offset = 0;
    read_and_update_offset(offset, buffer, parsed_structure.a);
    read_and_update_offset(offset, buffer, parsed_structure.b);
    read_and_update_offset(offset, buffer, parsed_structure.c);
    read_and_update_offset(offset, buffer, parsed_structure.d);

    // Print the values
    std::cout << 
        parsed_structure.a << " " <<
        parsed_structure.b << " " <<
        parsed_structure.c << " " <<
        parsed_structure.d << " " << std::endl;

    // Look the size of our structure is different than the size of our buffer due to alignment
    std::cout <<
        "sizeof(buffer)" << "==" << sizeof(buffer) << " " <<
        "sizeof(parsed_structure)" << "==" << sizeof(parsed_structure) << std::endl;

    return 0;
}
+4

- , nextInt, nextDouble .., ( "" ) , , . , . ( ++ vs Objective-C.)

+2

All Articles