Search code examples
pythoncsocketspackunpack

How to "unpack" packet from python (client) received in C (server)?


I have a python client who packs some data doing this:

#MOUNT UDP PACKET (unsigned char type, 5 char ext, 50 char user)
pqtUDP = pack('B5s50s', REGISTER, ext, user) 

And now I'm receiving that on a C client, so to read correct data I suppose I have to unpack it and save it in different vars, no? How can I do it in C?

I need to read REGISTER, ext and user from received data.


Solution

  • Something like this should work in C:

    // assumes input packet is [const char* data]
    unsigned char reg;
    char ext[6];
    char user[51];
    reg = data[0];
    memcpy(ext, data + 1, 5);
    ext[5] = 0; // set null terminator
    memcpy(user, data + 6, 50);
    user[50] = 0; // set null terminator