zuhu2195
(Mohammed Zuhaib)
March 6, 2024, 7:45am
1
Hi,
I am trying to read a byte string from python which is something as follows "
ZWLFABRIVERZMATZ \x00 Vers User"
since this string has NULL bytes when writing a C extension the API’s are removing the rest of the string.
the string is then interpreted as
"ZWLFABRIVERZMATZ "
I tried the following API’s
PyBytes_AsStringAndSize
PyBytes_AsString
is there anyway to extract the string even if it contains NULL bytes without any modification?
thanks in advance.
SIvaCoHan
(SivaCoHan)
March 6, 2024, 8:16am
2
Let ‘func_c’ is the C API function. You can you code like this。
p3 = ctypes.create_string_buffer(24)
func_c(p3)
print(p3.raw)
use ‘p3.raw’ can avoid truncate by null byte
zuhu2195
(Mohammed Zuhaib)
March 6, 2024, 8:17am
3
I would like to do it in C extension itself.
Do not use C strings, that are \0 terminated.
Use pointer and length APIs.
I use PyBytes_AsStringAndSize for this purpose.
It is documented to be able to return all the bytes without 0x00 being special.
zuhu2195
(Mohammed Zuhaib)
March 6, 2024, 9:16am
5
I am unable to do so for the string mentioned above, what should the format of Python Object be while extracting this from PyArg_ParseTupleAndKeywords
currently I am extracting string
b’ZWLFABRIVERZMATZ \x00 Vers User’
as python object ( ‘O’ format).
MegaIng
(Cornelius Krupp)
March 6, 2024, 9:23am
6
And what are you then doing with the string after python gave it to you? You can’t just used the C stdlib functions like printf, those will see the NUL byte still, CPython can’t do anything about that.
zuhu2195
(Mohammed Zuhaib)
March 6, 2024, 9:41am
7
thank you @MegaIng , yes I was using printf and unable to see the results, when printed individual characters they worked. Thank you @barry-scott .