[英]Python 2's .decode(“hex”)
我在理解Python 2的foo.decode("hex")
命令时遇到了一些麻烦。 解决此问题后,我在Python 2.7.12中获得了以下内容(其中words_alpha.txt是4 MB的字典)。
words = open("words_alpha.txt").read().split('\n')
def xor(x, y):
if len(x) == len(y):
return "".join([chr(ord(x[i]) ^ ord(y[i])) for i in range(len(x))])
def single_char_xors(msg):
for i in range(128):
yield [chr(i), xor(msg, chr(i)*len(msg))]
def real_word_count(S): # Assumes there is at least one three-letter word in the string S.
count = 0
for word in filter(lambda s: s.isalpha() and len(s) >= 3, S.split(' ')):
if word.lower() in words:
count += 1
return count
hexes = open("4.txt").read().split('\n')
hexes = [x.decode("hex") for x in hexes]
answer = []
maxwc = 0
for x in hexes:
for y in single_char_xors(x):
if real_word_count(y[1]) > maxwc:
answer = [x] + y
maxwc = real_word_count(y[1])
print answer[0] + " xor " + answer[1] + " is " + answer[2]
在Python 3中,不建议使用foo.decode("hex")
。 但是用hexes = [x.decode("hex") for x in hexes]
hexes = [binascii.unhexlify(x).decode() for x in hexes]
hexes = [x.decode("hex") for x in hexes]
替换hexes = [x.decode("hex") for x in hexes]
hexes = [binascii.unhexlify(x).decode() for x in hexes]
得到:
UnicodeDecodeError:'utf-8'编解码器无法解码位置3的字节0xe8:无效的继续字节
hexes = [binascii.unhexlify(x).decode("utf-8", "ignore") for x in hexes]
(或"replace"
, "backslashreplace"
等)可以正常工作。 那么,执行binascii.unhexlify(foo).decode()
默认情况下不执行的foo.decode("hex")
什么?
我相信问题出在.decode("utf-8", "ignore")
-使用"ignore"
参数,您实际上可以忽略在第一种情况下引发UnicodeDecodeError
异常的问题。
binascii.hexlify
和codecs.decode
之间的codecs.decode
:
己二酸
二进制数据的十六进制表示。
返回值是一个字节对象。
类型:builtin_function_or_method
编解码器
.decode(obj,encoding ='utf-8',errors ='strict')
使用注册用于编码的编解码器对obj进行解码。可以设置错误以设置所需的错误处理方案。 默认错误处理程序为“严格”,这意味着解码错误会引发ValueError(或更特定于编解码器的子类,例如UnicodeDecodeError)。 有关编解码器错误处理的更多信息,请参见编解码器基类。
类型:builtin_function_or_method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.