# Reading NULL bytes in c extensions

**URL:** <https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747>\
**Category:** Python Help\
**Tags:** help\
**Created:** [March 6, 2024, 7:45am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747 "2024-03-06T07:45:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 6, 2024, 7:45am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/1 "2024-03-06T07:45:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SIvaCoHan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sivacohan/32/16060_2.png) [@SIvaCoHan](https://discuss.python.org/u/SIvaCoHan)\
**Post date:** [March 6, 2024, 8:16am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/2 "2024-03-06T08:16:19Z")

</div>

Let ‘func\_c’ is the C API function. You can you code like this。

```python
p3 = ctypes.create_string_buffer(24)
func_c(p3)
print(p3.raw)

```

use ‘p3.raw’ can avoid truncate by null byte

---

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 6, 2024, 8:17am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/3 "2024-03-06T08:17:45Z")

</div>

I would like to do it in C extension itself.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [March 6, 2024, 8:49am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/4 "2024-03-06T08:49:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 6, 2024, 9:16am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/5 "2024-03-06T09:16:29Z")

</div>

> [@barry-scott](#):
>
> I use PyBytes\_AsStringAndSize for this purpose.  
> It is documented to be able to return all the bytes without 0x00 being special.

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).

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [March 6, 2024, 9:23am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/6 "2024-03-06T09:23:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 6, 2024, 9:41am UTC](https://discuss.python.org/t/reading-null-bytes-in-c-extensions/47747/7 "2024-03-06T09:41:05Z")

</div>

thank you @MegaIng, yes I was using printf and unable to see the results, when printed individual characters they worked. Thank you @barry-scott .
