1

I have a string bytecode like this "b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='"

and I want it as bytecode type like this b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='

3
  • the string byte code is in a variable. Commented May 5, 2022 at 12:17
  • The original string bytestring can be passed into ast.literal_eval and a bytestring (bytes) object will be returned. Commented May 5, 2022 at 12:32
  • 1
    It's worth noting that this isn't technically bytecode, it's a bytestring. Bytecode is the language that Python is compiled into. Commented May 15, 2022 at 23:28

1 Answer 1

2

The ast module can be used to convert the bytecode base64 str into a proper bytestring (bytes) object.

Example:

import ast

x = "b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='"
out = ast.literal_eval(x)

Output:

>>> out
b'gAAAAABicrsec2Ce4oBYs4pzEpEHou1ZkR4IHai76C9TJHHVk5KOtJ4d154qBhNqf9LGB4svlQkPHue2XNQJUtp8ZLe9dqzk8w=='

>>> type(out)
bytes
Sign up to request clarification or add additional context in comments.

Comments

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.