jim ying
jim ying

Reputation: 391

python how to pack struct with bit field width?

//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?

Upvotes: 4

Views: 6006

Answers (1)

Shlomo Gottlieb
Shlomo Gottlieb

Reputation: 593

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

Upvotes: 7

Related Questions