6
//c struct code with filed width:

    struct{
      unsigned int x:1;
      unsigned int y:25;
      unsigned int z:6;
    };

Now I want to rewrite it in python, pack it and send to network,

The package struct in python, can pack/unpack data types.

such as:

struct.pack('!I10s',1,'hello')

But I don't know how to deal with struct with filed width as the c struct example. Any one know?

3

1 Answer 1

8

I realize it's late, but the following could help newcomers.

You can combine ctypes with struct.pack_into/struct.unpack_from:

import struct
import ctypes


class CStruct(ctypes.BigEndianStructure):
    _fields_ = [
        ("x", ctypes.c_uint32, 1),  # 1 bit wide
        ("y", ctypes.c_uint32, 25), # 25 bits wide
        ("z", ctypes.c_uint32, 6)   # 6 bits wide
    ]


cs = CStruct()

# then you can pack to it:
struct.pack_into('!I', cs,
                 0, # offset
                 0x80000083)
print(f'x={cs.x} y={cs.y} z={cs.z}')
# x=1 y=2 z=3

# or unpack from it:
cs_value, = struct.unpack_from('!I', cs)
print(hex(cs_value))
# 0x80000083
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.