[英]What is a difference in XOR function between python2 and python3?
我有两个字符串:
string1 = "\xc5\x06\x92\xd0\x02k=\x91"
string2 = "qwert\00\00\00"
和 function:
def xor(str1,str2):
ret = ''
for i in range(8):
ret += chr(ord(str1[i]) ^ ord(str2[i]))
return ret
上述function的结果是:
在 python2.7 中: ´�q��vk=�
; 十六进制: ef bf bd 71 ef bf bd ef bf bd 76 6b 3d ef bf bd
在 python3.6 中´q÷¢vk='
; 十六进制: b4 71 f7 a2 76 6b 3d 91
我想这与 python2 str
类型仅限于 ascii 的事实有关,但是如何在两个版本中获得相同的值?
两个版本中的值相同。 您只是在不支持某些字符的语言环境中打印它,并且它使用 Unicode 替换字符来显示它(output 中的ef bf bd
序列是它无法识别的字符成为替换的地方字符;您用来转换为字节的任何内容都用其 UTF-8 编码无缝替换了 Unicode 替换字符)。
当区域设置正确并且您具有处理结果的终端/字体支持时,它在Python 2和Python 3上的工作方式相同。 The only real difference is that Python 3 has somewhat saner behaviors under some locales (eg Windows console using UTF-8 automatically in 3.6, legacy C locale coercion in 3.7), but you got the same string, it's just outputting and displaying it that produces the wrong结果,同时试图避免不可编码的字符。
需要明确的是,Python 2 str
不限于 ASCII。 就它所能容纳的内容而言,相当于 Python 3 bytes
; 两者都可以保存 [0, 256) 范围内的任意值。 文字不同(Py2 允许文字中的非 ASCII 字符没有转义,但没有文件编码声明,它不可移植),但 Py2 str
可以像 Py3 bytes
's b'\xff'
一样保存'\xff'
\xff' 。
请注意,当str
包含未使用转义插入的 ASCII 范围之外的字符时,您的代码通常不会以相同的方式工作(它取决于文件的编码声明,字符串文字中的非 ASCII 文字字符对 Python 2 的含义) ),并且对于不在 latin-1 中的东西肯定不会起作用(因为它在 Py3 中的序数大于 256,谁知道在 Py2 中是什么),除非输入是unicode
类型的 Python 2 (例如文字,以u
为前缀)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.