c与python之间传递特殊字符

各位大侠。我现在有以下使用情况,从C语言传递abc\0xyz给python,从python传递给C语言侧xyz\0abc。就是在c和python之间传递中间有\0的连续字符。如果按字符串传递,会被截断。如何才能保留不被截断呢?谢谢!

>>> from ctypes import *
>>> libc=cdll.msvcrt
>>> libc.printf("hello %s\n","world")
hello world
12
>>>

>>> p = create_string_buffer("Hello", 30)
>>> n=libc.sprintf(p,"hello %s\n","world")
>>> n
12
>>> print p
<ctypes.c_char_Array_30 object at 0x02247C10>
>>> dir(p)
['__class__', '__ctypes_from_outparam__', '__delattr__', '__delitem__', '__delsl
ice__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__',
'__getslice__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__r
educe__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setslice
__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_b_base_', '_b_needsfree_', '_length_', '_objects', '_type_', 'raw', 'value']
>>> p.value
'hello world\n'
>>>追问

谢谢!我的应用的条件是C api,不能使用ctype。该如何处理呢?

温馨提示:答案为网友推荐,仅供参考
相似回答