简体   繁体   English

如何将C ++中的空字符序列转换为Python中的等效字符序列?

[英]How can I convert an empty character sequence in C++ to its equivalent in Python?

I am using ctypes in Python to access functions from a shared C library. 我在Python中使用ctypes来访问共享C库中的函数。 One of the parameters for one of the functions requires an empty array of the type char to write error messages into. 其中一个函数的参数之一需要char类型的空数组来写入错误消息。 The code for the array in C++ is simple; C ++中数组的代码很简单;

char messageBuffer[256];

I would like to write the equivalent in Python so that I can pass an array containing the correct data type to the function. 我想在Python中编写等价物,以便我可以将包含正确数据类型的数组传递给函数。 I've tried simply creating and array in Python of length 256, but I get the error message; 我试过简单地用长度为256的Python创建和数组,但是我得到了错误信息;

mydll.TLBP2_error_message(m_handle, errorCode, messageBuffer)

ArgumentError: argument 3: <class 'TypeError'>: Don't know how to convert parameter 3

Any help is appreciated. 任何帮助表示赞赏。

you can use create_string_buffer() function in ctype package 你可以在ctype包中使用create_string_buffer()函数

If you need mutable memory blocks, ctypes has a create_string_buffer() function which creates these in various ways. 如果你需要可变内存块,ctypes有一个create_string_buffer()函数,它以各种方式创建它们。 The current memory block contents can be accessed (or changed) with the raw property; 可以使用raw属性访问(或更改)当前内存块内容; if you want to access it as NUL terminated string, use the value property: 如果要以NUL终止字符串的形式访问它,请使用value属性:

 >>> from ctypes import * >>> p = create_string_buffer(3) # create a 3 byte buffer, initialized to NUL bytes >>> print sizeof(p), repr(p.raw) 3 '\\x00\\x00\\x00' >>> p = create_string_buffer("Hello") # create a buffer containing a NUL terminated string >>> print sizeof(p), repr(p.raw) 6 'Hello\\x00' >>> print repr(p.value) 'Hello' >>> p = create_string_buffer("Hello", 10) # create a 10 byte buffer >>> print sizeof(p), repr(p.raw) 10 'Hello\\x00\\x00\\x00\\x00\\x00' >>> p.value = "Hi" >>> print sizeof(p), repr(p.raw) 10 'Hi\\x00lo\\x00\\x00\\x00\\x00\\x00' >>> 

To create a mutable memory block containing unicode characters of the C type wchar_t use the create_unicode_buffer() function. 要创建包含C类型wchar_t的unicode字符的可变内存块,请使用create_unicode_buffer()函数。

for more information refer: ctype-fundamental-data-types 有关更多信息,请参阅:ctype-fundamental-data-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++ 中将 Python 字符串转换为其转义版本? - How to convert a Python string into its escaped version in C++? 如何将Python布尔对象转换为C int(或C ++ boolean)(Python C API) - How can I convert Python boolean object to C int (or C++ boolean) (Python C API) 如何在python中将unicode转换为其原始字符 - How to convert unicode to its original character in Python 如何在 Python 的 URL 中发出包含“à”字符的请求? - How can I make a request including "à" character in its URL in Python? 如何在Python中从其名称中确定Unicode字符,即使该字符是控制字符? - How can I determine a Unicode character from its name in Python, even if that character is a control character? Python 3:如何在变量中取值并将其转换为 unicode 字符? - Python 3: How can I take a value in a variable and convert that to a unicode character? 如何将字符转换为 Python 中的 integer,反之亦然? - How can I convert a character to a integer in Python, and viceversa? 如何在python中转换“ \\ u0096”之类的字符? - How can I convert character like “\u0096” in python? Python-如何将特殊字符转换为unicode表示形式? - Python - How can I convert a special character to the unicode representation? 如何使用范围将这个 python for 循环转换为 C++? - How can I convert this python for-loop using a range into C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM