我正在尝试将由socket.inet_pton返回的16字节数据块转换为无符号字节的ctypes数组。我的数据结构如下:
class in6_addr(ctypes.Structure): _fields_ = (("Byte", ctypes.c_ubyte * 16),)而这个数据块只是:
data = socket.inet_pton(socket.AF_INET6, "2001::3")然而,以下尝试会导致错误:
sin6 = in6_addr()所有代码:http://codepad.org/2cjyVXBA 有什么想法需要转换成什么类型吗?
# TypeError: expected c_ubyte_Array_16 instance, got str sin6.Byte = data # TypeError: cast() argument 2 must be a pointer type, not c_ubyte_Array_16 sin6.Byte = ctypes.cast(data, ctypes.c_ubyte * 16) # TypeError: incompatible types, LP_c_ubyte instance instead of c_ubyte_Array_16 instance sin6.Byte = ctypes.cast(data, ctypes.POINTER(ctypes.c_ubyte))
sin6.Byte = (ctypes.c_ubyte * 16)(*data)
- Mark Tolonen