gpt4 book ai didi

python - Python 3 和 Python 2 之间的 XOR 函数差异?

转载 作者:行者123 更新时间:2023-11-28 18:03:46 25 4
gpt4 key购买 nike

在 IDLE、Python 3.6.5 和 Python 2.7.15 中运行我遇到了一个关于 XOR 的奇怪问题。我用 Python 2.7 得到了正确答案,用 Python 3.6 得到了垃圾。 Python 3.6 和 2.7 没有就简单的 XOR 达成一致。这不是 IDLE 问题,因为在 cygwin 中行为是相同的。

>>> ciphertext

'466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83'

python 2.7

>>> ciphertext.decode('hex')
'Fm\x06\xec\xe9\x98\xb7\xa2\xfb\x1dFO\xed,\xedvA\xdd\xaa<\xc3\x1c\x99A\xcf\x11\n\xbb\xf4\t\xed9Y\x80\x05\xb39\x9c\xcf\xaf\xb6\x1d\x03\x15\xfc\xa0\xa3\x14\xbe\x13\x8a\x9f2P;\xed\xac\x80g\xf0:\xdb\xf3W\\;\x8e\xdc\x9b\xa7\xf57S\x05A\xab\x0f\x9f<\xd0O\xf5\rf\xf1\xd5Y\xbaR\x0e\x89\xa2\xcb*\x83'
>>> for x, y in zip(ciphertext.decode('hex'), ' '*10):
print "ord(x): " + chr(ord(x))
print "ord(y): " + chr(ord(y))
print(chr(ord(x) ^ ord(y)))


ord(x): F
ord(y):
f
ord(x): m
ord(y):
M
ord(x):
ord(y):
&
ord(x): ì
ord(y):
Ì
ord(x): é
ord(y):
É
ord(x): ˜
ord(y):
¸
ord(x): ·
ord(y):

ord(x): ¢
ord(y):

ord(x): û
ord(y):
Û
ord(x):
ord(y):
=

python 3.6

 >>> bytes.fromhex(ciphertext)

b'Fm\x06\xec\xe9\x98\xb7\xa2\xfb\x1dFO\xed,\xedvA\xdd\xaa<\xc3\x1c\x99A\xcf\x11\n\xbb\xf4\t\xed9Y\x80\x05\xb39\x9c\xcf\xaf\xb6\x1d\x03\x15\xfc\xa0\xa3\x14\xbe\x13\x8a\x9f2P;\xed\xac\x80g\xf0:\xdb\xf3W\\;\x8e\xdc\x9b\xa7\xf57S\x05A\xab\x0f\x9f<\xd0O\xf5\rf\xf1\xd5Y\xbaR\x0e\x89\xa2\xcb*\x83'
>>> for x, y in zip(bytes.fromhex(ciphertext), ' '*10):
print("x: ", chr(x))
print("ord(y): ", chr(ord(y)))
print(chr(x^ord(y)))


x: F
ord(y):
f
x: m
ord(y):
M
x:
ord(y):
&
x: ì
ord(y):
Ì
x: é
ord(y):
É
x:
ord(y):
¸
x: ·
ord(y):
<- different value
x: ¢
ord(y):
<- different value
x: û
ord(y):
Û
x:
ord(y):
=

In a cygwin window I get the following:
$ ./python2_test.py
Fm▒阷▒▒FO▒,▒vAݪ<▒▒A▒
f▒▒Y▒R▒▒▒*▒▒▒9▒ϯ▒▒▒▒▒▒▒2P;▒g▒:▒▒W\;▒ܛ▒▒7SA▒▒<▒O▒
ord(x): F
ord(y):
f
ord(x): m
ord(y):
M
ord(x):
ord(y):
&
ord(x): ▒
ord(y):

ord(x): ▒
ord(y):

ord(x): ▒
ord(y):

ord(x): ▒
ord(y):

ord(x): ▒
ord(y):

ord(x): ▒
ord(y):

ord(x):
ord(y):
=
$ ./python3_test.py
b'Fm\x06\xec\xe9\x98\xb7\xa2\xfb\x1dFO\xed,\xedvA\xdd\xaa<\xc3\x1c\x99A\xcf\x11\n\xbb\xf4\t\xed9Y\x80\x05\xb39\x9c\xcf\xaf\xb6\x1d\x03\x15\xfc\xa0\xa3\x14\xbe\x13\x8a\x9f2P;\xed\xac\x80g\xf0:\xdb\xf3W\\;\x8e\xdc\x9b\xa7\xf57S\x05A\xab\x0f\x9f<\xd0O\xf5\rf\xf1\xd5Y\xbaR\x0e\x89\xa2\xcb*\x83'
x: F
ord(y):
f
x: m
ord(y):
M
x:
ord(y):
&
x: ì
ord(y):
Ì
x: é
ord(y):
É
x:
ord(y):
¸
x: ·
ord(y):
<- missing
x: ¢
ord(y):
<- missing
x: û
ord(y):
Û
x:
ord(y):
=

如能帮助解决此问题,我们将不胜感激。干杯。

最佳答案

我认为您遇到了编码问题。如果您尝试重现打印数字而不是字符的步骤,您将看不出有什么不同。这些是脚本:

Python2:

ciphertext = '466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83'

out1 = []
out2 = []
out3 = []
for x, y in zip(ciphertext.decode('hex'), ' '*10):
out1.append(hex(ord(x)))
out2.append(hex(ord(y)))
out3.append(hex(ord(x) ^ ord(y)))
print out1
print out2
print out3

python 3:

ciphertext = '466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83'
out1 = []
out2 = []
out3 = []
for x, y in zip(bytes.fromhex(ciphertext), ' '*10):
out1.append(x)
out2.append(ord(y))
out3.append(x ^ ord(y))
print(out1)
print(out2)
print(out3)

如果同时执行它们,您会看到输出是相同的。

ideone上直接可以看到,python2python3

编辑:我给出的脚本的执行,稍微修改以显示十六进制而不是原始数字,给我作为输出:

Python2:

['0x46', '0x6d', '0x6', '0xec', '0xe9', '0x98', '0xb7', '0xa2', '0xfb', '0x1d']
['0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20']
['0x66', '0x4d', '0x26', '0xcc', '0xc9', '0xb8', '0x97', '0x82', '0xdb', '0x3d']

Python3:

['0x46', '0x6d', '0x6', '0xec', '0xe9', '0x98', '0xb7', '0xa2', '0xfb', '0x1d']
['0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20']
['0x66', '0x4d', '0x26', '0xcc', '0xc9', '0xb8', '0x97', '0x82', '0xdb', '0x3d']

使用您提供的相同输入数据,我有不同的输入,但我的输出数据在 Python2 和 Python3 之间是一致的。

关于python - Python 3 和 Python 2 之间的 XOR 函数差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54786021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com