General information about the task:
I need to write the function on C language which takes a byte array (it reperesents package) parse it, do some stuff and return a changed byte array.
My approach:
"filename.h"
char* ParsePackage(const char* byteArray);
typedef struct
{
char name[4];
float value;
} packageStructure;
I use the struct packageStructure to which a byteArray is casted, then I am trying to get data by accessing fields of that struct: "filename.cpp"
include "filename.cpp"
char* ParsePackage(const char* byteArray)
{
packageStructure* tmp = (packageStructure*) byteArray;
// get values of structure fields and do some staff with them:
tmp->name;
tmp->value;
return (char*)modifiedByteArray;
}
I am not satisfied with the result, because the whole data from byte array is written to the first field of the struct, which is a name, to the second filed goes some random value;
So expected questions here are: that I am doing wrong (how to alter my approach to make it work)? Can you offer other methods of parsing byte array?
thanks in advance!