0

I am writing a test that is intended to mutate binary data and ensure that my program can read the variations. Null bytes are an important part of this binary protocol.

When I try to save it, however, I encounter the error:

ValueError: source code string cannot contain null bytes

The assignment I'm currently trying is:

binary_blob = rb"""<value>"""

Where <value> has the hex representation 0x00.

How can I modify the assignment to avoid this error? I am using Python 3.9.5.

sentientcabbage
  • 466
  • 2
  • 6
  • 3
    You can't put literal null bytes in the source code. Use `\0` in the string literal. – Barmar Jul 16 '21 at 19:20
  • You can do `binary_blob = bytes([0x00])` if you want to create a `bytes` with a `0x00` byte. – Samwise Jul 16 '21 at 19:22
  • `'\0'`, `'\u0000'`, and `'\x00'` are all different ways to create a NULL character. –  Jul 16 '21 at 19:25
  • Does this answer your question? [Python: source code string cannot contain null bytes](https://stackoverflow.com/questions/31233777/python-source-code-string-cannot-contain-null-bytes) – ti7 Jul 16 '21 at 19:38
  • "When I try to save it, however, I encounter the error", what, **exactly** are you doing? – juanpa.arrivillaga Jul 16 '21 at 19:42

1 Answers1

2

You're probably looking for the null character, which you can create with \0

>>> b"\0"
b'\x00'
>>> ord("\0")
0
ti7
  • 16,375
  • 6
  • 40
  • 68