How can I determine the used size/length of a buffer created with create_string_buffer?
buffer = create_string_buffer(1000)
e.g. in buffer I have 3 values and 997 unused -> how it's possible to get the 3?
How can I determine the used size/length of a buffer created with create_string_buffer?
buffer = create_string_buffer(1000)
e.g. in buffer I have 3 values and 997 unused -> how it's possible to get the 3?
你有什么理由不能做len(buffer.value)
吗?
为了纯粹的理论上的乐趣——如果你可以使用上述方法,我认为没有理由这样做——我会提到你也可以做buffer.raw.find("\x00")
or list(buffer).index("\x00")
。但是,如果字符串未正确以空值结尾,则这些失败。
你可以:
In [1]: from ctypes import *
In [2]: s = create_string_buffer(1000)
In [3]: s[0] = '1'
In [4]: s[1] = '2'
In [5]: s[2] = '3'
In [6]: s
Out[6]: <ctypes.c_char_Array_1000 at 0x8ca126c>
In [7]: for i, c in enumerate(s):
...: if c == '\x00':
...: break
...:
In [8]: i
Out[8]: 3
C 字符串以空值结尾,因此循环遍历字符串,直到找到 '\x00'。