1
str1 = "b""T\xefD\xcdB\x95\x90v\x9f\x88^\xa3\x08\x00E\x00\x00|\xd7u@\x00.\x11\x80\xcf\x0e\xd7#\x07\xc0\xa8\x01\xa6\x1fu\xb2d\x00h}\x0e!1\x00`\x00\x00\x00\x00\x16So\xe7`\x00|\xd0j\xcc\xbc\xf5<j\x807\x98\xb1t\xddMMrH\xf1\xffy\xbd]\x88\xb1\x9f\xe04r\xaaD\xc8\xd5\x9cc\xe1\xc9'\x0e\x1dsY)4\xaeq\xf9\xe9\x8e\xc7c\xd9O\xa5I\x86\xd6p\xa5.a){\xbc\x16\xe4\xf9\x86\x15\x7f\xddw!\xa8U#I\xf5FV.\xe0k\xcf"""
b1 = b'T\xefD\xcdB\x95\x90v\x9f\x88^\xa3\x08\x00E\x00\x00|\xd7u@\x00.\x11\x80\xcf\x0e\xd7#\x07\xc0\xa8\x01\xa6\x1fu\xb2d\x00h}\x0e!1\x00`\x00\x00\x00\x00\x16So\xe7`\x00|\xd0j\xcc\xbc\xf5<j\x807\x98\xb1t\xddMMrH\xf1\xffy\xbd]\x88\xb1\x9f\xe04r\xaaD\xc8\xd5\x9cc\xe1\xc9'\x0e\x1dsY)4\xaeq\xf9\xe9\x8e\xc7c\xd9O\xa5I\x86\xd6p\xa5.a){\xbc\x16\xe4\xf9\x86\x15\x7f\xddw!\xa8U#I\xf5FV.\xe0k\xcf'

how to transfer str1 which is <class 'str'> to b1 which is <class 'bytes'>?

3
  • 1
    Your "b""T\xefD\xcd… will actually be concatenated to "bT\xefD\xcd… Commented Jun 22, 2021 at 8:58
  • Is there a format error in str1? Commented Jun 22, 2021 at 9:08
  • It is not exactly an error, but written like this the string becomes something different than what might be expected. It's not clear to us what the input data is exactly. Commented Jun 22, 2021 at 9:11

2 Answers 2

2

Here's a simple way:

str1 = "b""T\xefD\xcdB\x95\x90v\x9f\x88^\xa3\x08\x00E\x00\x00|\xd7u@\x00.\x11\x80\xcf\x0e\xd7#\x07\xc0\xa8\x01\xa6\x1fu\xb2d\x00h}\x0e!1\x00`\x00\x00\x00\x00\x16So\xe7`\x00|\xd0j\xcc\xbc\xf5<j\x807\x98\xb1t\xddMMrH\xf1\xffy\xbd]\x88\xb1\x9f\xe04r\xaaD\xc8\xd5\x9cc\xe1\xc9'\x0e\x1dsY)4\xaeq\xf9\xe9\x8e\xc7c\xd9O\xa5I\x86\xd6p\xa5.a){\xbc\x16\xe4\xf9\x86\x15\x7f\xddw!\xa8U#I\xf5FV.\xe0k\xcf"""
    
b1 = bytes(str1.split("b")[1], 'raw_unicode_escape')

print (b1)

Output:

b"T\xefD\xcdB\x95\x90v\x9f\x88^\xa3\x08\x00E\x00\x00|\xd7u@\x00.\x11\x80\xcf\x0e\xd7#\x07\xc0\xa8\x01\xa6\x1fu\xb2d\x00h}\x0e!1\x00\x00\x00\x00\x00\x16So\xe7\x00|\xd0j\xcc\xbc\xf5<j\x807\x98\xb1t\xddMMrH\xf1\xffy\xbd]\x88\xb1\x9f\xe04r\xaaD\xc8\xd5\x9cc\xe1\xc9'\x0e\x1dsY)4\xaeq\xf9\xe9\x8e\xc7c\xd9O\xa5I\x86\xd6p\xa5.a){\xbc\x16\xe4\xf9\x86\x15\x7f\xddw!\xa8U#I\xf5FV.\xe0k\xcf"

Sign up to request clarification or add additional context in comments.

Comments

1

if you have a correct format str1, you can try to use eval

code:

str1 = r'b"T\xefD\xcdB\x95\x90v\x9f\x88^\xa3\x08\x00E\x00\x00|\xd7u@\x00.\x11\x80\xcf\x0e\xd7#\x07\xc0\xa8\x01\xa6\x1fu\xb2d\x00h}\x0e!1\x00`\x00\x00\x00\x00\x16So\xe7`\x00|\xd0j\xcc\xbc\xf5<j\x807\x98\xb1t\xddMMrH\xf1\xffy\xbd]\x88\xb1\x9f\xe04r\xaaD\xc8\xd5\x9cc\xe1\xc9\'\x0e\x1dsY)4\xaeq\xf9\xe9\x8e\xc7c\xd9O\xa5I\x86\xd6p\xa5.a){\xbc\x16\xe4\xf9\x86\x15\x7f\xddw!\xa8U#I\xf5FV.\xe0k\xcf"'
x = eval(str1)
print(x)
print(type(x))

result:

b"T\xefD\xcdB\x95\x90v\x9f\x88^\xa3\x08\x00E\x00\x00|\xd7u@\x00.\x11\x80\xcf\x0e\xd7#\x07\xc0\xa8\x01\xa6\x1fu\xb2d\x00h}\x0e!1\x00`\x00\x00\x00\x00\x16So\xe7`\x00|\xd0j\xcc\xbc\xf5<j\x807\x98\xb1t\xddMMrH\xf1\xffy\xbd]\x88\xb1\x9f\xe04r\xaaD\xc8\xd5\x9cc\xe1\xc9'\x0e\x1dsY)4\xaeq\xf9\xe9\x8e\xc7c\xd9O\xa5I\x86\xd6p\xa5.a){\xbc\x16\xe4\xf9\x86\x15\x7f\xddw!\xa8U#I\xf5FV.\xe0k\xcf" 
<class 'bytes'>

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.