t = '.EA.B2.80.EB.A7.88'
t = t.replace('.','\\x').lower()
string1 = b"\xea\xb2\x80\xeb\xa7\x88"
print(string1.decode("utf-8"))
string2 = bytes(t, "utf-8")
print(string2)
print(string2.decode("utf-8"))
String1 returned the decoded value I wanted, but string2 returned two backslashes. I think string1 and string2 are not different but wonder why string2 returns two backslashes. Can anybody answer this? Thank you for reading!
In string1
, the backslashes are part of the syntax of a bytes
literal, used to produce the correct bytes in the resulting value.
You are adding literal backslashes to the value of the string t
.
I'm not sure this is the best solution, but you can convert t
to a sequence of int
values which can be passed to bytes
.
string2 = bytes(int(x, 16) for x in t.strip(".").split("."))
Update: there is something simpler:
string2 = bytes.fromhex(t.replace(".", ""))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.