Different interpretation of hex strings in python

吃可爱长大的小学妹 提交于 2020-04-18 06:08:25

问题


In the past few days, I've been struggling to understand why this piece of codes behaves in such a way:

code:

file1 = open("input.txt","r")
M = file1.read()
file1.close()
print(M)
print(M.encode("latin"))
print(type(M.encode("latin")))
print("\n-----------------------------\n")
t = "\xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59"
print(t)
print(t.encode("latin"))
print(type(t.encode("latin")))

file "input.txt" content:

\xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59

output:

\xAC\x42\x4C\x45\x54\x43\x48\x49\x4E\x47\x4C\x45\x59
b'\\xAC\\x42\\x4C\\x45\\x54\\x43\\x48\\x49\\x4E\\x47\\x4C\\x45\\x59'
<class 'bytes'>

-----------------------------

¬BLETCHINGLEY
b'\xacBLETCHINGLEY'
<class 'bytes'>

What I don't understand is why the same string is interpreted in 2 different ways, if I read it from the file or if I copy it (by hands) in a variable. I know that the double "\" is probably the result of me printing the string to the console, but I cannot understand what is happening.

来源:https://stackoverflow.com/questions/60811088/different-interpretation-of-hex-strings-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!