Q:

How to struct unpack c null terminated string?

I used tcp to send a data to python server. The data is like:
struct protocol
{
    unsigned char prot;
    int id;
    char name[32];
}

Look at the name field, it is a null terminated string max size is 32. Now I use strcpy.
protocol p;
memset(&p, 0, sizeof(p));
strcpy(name, "abc");

Now I unpack it using python.
prot,id,name = struct.unpack("@Bi32s")

Now the len(name) is 32. But I need get the string of "abc" when the length is 3.
How can I do that?

A:

After the unpacking you can just do a:
name = name.split('\0', 1)[0]

Alternatively you could use the ctypes module:
name = ctypes.create_string_buffer(name).value

