Python 如何打印ctypes字符串缓冲区的内容

Python 如何打印ctypes字符串缓冲区的内容,python,ctypes,Python,Ctypes,我正在使用python中的ctypes库创建一个字符串缓冲区。 现在,如果我必须在编写这个字符串缓冲区之后打印它的内容,我将如何在python中实现这一点 import ctypes init_size = 256 pBuf = ctypes.create_string_buffer(init_size) 您可以使用.value和.raw属性来访问和操作它们。这一点在很早以前就有了记录 下面是该部分的一些示例代码: >>> from ctypes import * >&

我正在使用python中的ctypes库创建一个字符串缓冲区。 现在,如果我必须在编写这个字符串缓冲区之后打印它的内容,我将如何在python中实现这一点

import ctypes
init_size = 256
pBuf = ctypes.create_string_buffer(init_size)

您可以使用
.value
.raw
属性来访问和操作它们。这一点在很早以前就有了记录

下面是该部分的一些示例代码:

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

根据Python Ctypes文档,位于:

您应该能够使用.value对象属性打印字符串缓冲区值,即:

print repr(pBuf.value) 
或者,如果您想对io有一点兴趣,可以使用以下内容:

print "pBuff: %s" % pBuf.value