python中char的用法_python char_使用ctypes的Python中访问c_char_p_Array_256

23894

I have a native python bridge to some C code, which returns a pointer to an array (array of structs). The struct contains some character arrays (strings). But how can I get from a c_char_p_Array_NNN to an actual Python string?

typedef struct td_Group

{

unsigned int group_id;

char groupname[256];

char date_created[32];

char date_modified[32];

unsigned int user_modified;

unsigned int user_created;

} Group;

int getGroups(LIBmanager *, Group **);

############# python code below:

class Group(Structure):

_fields_ = [("group_id", c_uint),

("groupname", c_char_p*256),

("date_created", c_char_p*32),

("date_modified", c_char_p*32),

("user_modified", c_uint),

("user_created", c_uint)]

def somefunc():

myGroups = c_void_p()

count = libnativetest.getGroups( nativePointer, byref(myGroups) )

print "got " + str(count) + " groups!!"

myCastedGroups = cast( myGroups, POINTER(Group*count) )

for x in range(0,count):

theGroup = cast( myCastedGroups[x], POINTER(Group) )

theGroupName = theGroup.contents.groupname

### Now how do I access theGroupName as a python string?

# it is a c_char_p_Array_256 presently

解决方案

The type should be c_char*256, not c_char_p*256, because it's a char[256], not a char *[256].

string_at(theGroupName, sizeof(theGroupName))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值