如何将通过 ctype malloc 分配的二进制缓冲区保存到 Python 中的文件?

发布于 2024-12-11 01:48:54 字数 340 浏览 0 评论 0原文

我有以下代码

import ctypes
pBuf = ctypes.cdll.msvcrt.malloc(nBufSize)
# wrote something into the buffer

如何使用 Python 2.5 将缓冲区的内容保存到文件中?

您可能已经知道,这是行不通的,给出 TypeError: argument 1 must be string or read-only buffer, not int

f = open("out.data","wb"
f.write(pBuf)

I have the following code

import ctypes
pBuf = ctypes.cdll.msvcrt.malloc(nBufSize)
# wrote something into the buffer

How do I save the content of the buffer to a file using Python 2.5?

As you may already know, this is not going to work, giving TypeError: argument 1 must be string or read-only buffer, not int:

f = open("out.data","wb"
f.write(pBuf)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-12-18 01:48:54

也许最好用 ctypes.create_string_buffer()< 分配缓冲区/code>而不是 malloc()。在这种情况下,您可以通过 buf.raw 访问数据。

如果您需要访问 malloc()ed 数据,可以使用 ctypes.string_at(address, size), mybe 与转换为 ctypes.c_void_pctypes.c_char_p,取决于您对内存执行的其他操作以及其中包含的内容(\0 终止的字符串或已知长度的数据)。

Maybe it would better to have the buffer allocated with ctypes.create_string_buffer() instead of malloc(). In this case, you have access to the data via buf.raw.

If you need access to malloc()ed data, you can do so with ctypes.string_at(address, size), mybe combined with a cast to ctypes.c_void_p or ctypes.c_char_p, depending on what else you do with the memory and what is contained (\0 terminated string or data with known length).

秋凉 2024-12-18 01:48:54

将缓冲区转换为指向字节数组的指针,然后从中获取值。另外,如果您使用的是 64 位系统,则需要确保将 malloc 的返回类型设置为 c_void_p(不是默认的 >int),这样返回值就不会丢失任何位。

您还需要小心,以防数据中嵌入了 NUL - 您不能只将指针转换为 c_char_p 并将其转换为字符串(如果是这样,则尤其如此)你的数据根本不是以 NUL 结尾的)。

malloc = ctypes.dll.msvcrt.malloc
malloc.restype = ctypes.c_void_p

pBuf = malloc(nBufSize)
...
# Convert void pointer to byte array pointer, then convert that to a string. 
# This works even if there are embedded NULs in the string.
data = ctypes.cast(pBuf, ctypes.POINTER(ctypes.c_ubyte * nBufSize))
byteData = ''.join(map(chr, data.contents))

with open(filename, mode='wb') as f:
    f.write(byteData)

Cast the buffer into a pointer to a byte array, and then get the value from that. Also, if you're on a 64-bit system, you'll need to make sure to set the return type of malloc to a c_void_p (not the default int) so that the return value doesn't lose any bits.

You'll also need to be careful, in case there are embedded NULs in your data -- you can't just convert the pointer into a c_char_p and convert that into a string (which is especially true if your data isn't NUL-terminated at all).

malloc = ctypes.dll.msvcrt.malloc
malloc.restype = ctypes.c_void_p

pBuf = malloc(nBufSize)
...
# Convert void pointer to byte array pointer, then convert that to a string. 
# This works even if there are embedded NULs in the string.
data = ctypes.cast(pBuf, ctypes.POINTER(ctypes.c_ubyte * nBufSize))
byteData = ''.join(map(chr, data.contents))

with open(filename, mode='wb') as f:
    f.write(byteData)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文