我是靠谱客的博主 开心嚓茶,最近开发中收集的这篇文章主要介绍python char_使用ctypes的Python中访问c_char_p_Array_256,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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))

最后

以上就是开心嚓茶为你收集整理的python char_使用ctypes的Python中访问c_char_p_Array_256的全部内容,希望文章能够帮你解决python char_使用ctypes的Python中访问c_char_p_Array_256所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部