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.

5
  • 3
    You can't put literal null bytes in the source code. Use \0 in the string literal. Commented Jul 16, 2021 at 19:20
  • You can do binary_blob = bytes([0x00]) if you want to create a bytes with a 0x00 byte. Commented Jul 16, 2021 at 19:22
  • '\0', '\u0000', and '\x00' are all different ways to create a NULL character. Commented Jul 16, 2021 at 19:25
  • Does this answer your question? Python: source code string cannot contain null bytes Commented Jul 16, 2021 at 19:38
  • "When I try to save it, however, I encounter the error", what, exactly are you doing? Commented Jul 16, 2021 at 19:42

1 Answer 1

2

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

>>> b"\0"
b'\x00'
>>> ord("\0")
0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer! I converted my binary data to hex using xxd and was able to process it successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.