将十六进制字符串转换(解码)为ASCII或任何其他可理解的格式

时间:2015-04-09 19:19:20

标签: python python-3.x hex ascii type-conversion

b'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

我想将此十六进制字符串转换为ascii或可读文本。我从系统映像文件中获取此块。

2 个答案:

答案 0 :(得分:1)

'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'.decode("hex")

至少在py2x中

in 3x

bytes.fromhex(b'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'.decode("ascii"))

答案 1 :(得分:0)

扩展Joran的回答,

import string
data = bytes.fromhex(b'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'.decode("ascii"))
print("".join(chr(c) if chr(c) in string.printable else '.' for c in data))

结果:

vhdxfileM.i.c.r.o.s.o.f.t. .W.i.n.d.o.w.s. .6...3...9.6.0.0...1.7.3.9.6.........................................................................................................................................................................................

请注意,这不会为您提供完美的无损数据表示 - 不可读的字符会被句点替换。因此,如果您正在搜索文本数据,它才真正适用。