转换(解码)十六进制字符串为ASCII或任何其他可理解的格式 [英] Convert(decode) hex string to ASCII or any other understandable format

查看:150
本文介绍了转换(解码)十六进制字符串为ASCII或任何其他可理解的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  b'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' 



我想转换此十六进制字符串为ascii或可读文本。

解决方案

在Joran的回答中展开,

'pre> 进口字符串
数据= bytes.fromhex(b'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'.decode( ASCII))
打印( 。加入( chr(c)if chr(c)in string.printable else'。'for c in data))

结果:

  vhdxfileM.icrosoft 。视窗。 0.6 ... 3 ... ... 9.6.0.0 .............................. 1.7.3.9.6 .................................................. .................................................. .................................................. ..... 

请注意,这不会给您数据的完美无损表示 - 无法读取的字符被替换为句点。所以它只有在搜索文本数据时才真正适用。


b'7668647866696c654d006900630072006f0073006f00660074002000570069006e0064006f0077007300200036002e0033002e0039003600300030002e003100370033003900360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

I want to convert this hex string to ascii or readable text. I am getting this block from a system image file.

解决方案

Expanding on Joran's answer,

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

Result:

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.........................................................................................................................................................................................

Note that this won't give you a perfect lossless representation of the data - unreadable characters get replaced with periods. So it's only really suitable if you're searching for textual data.

这篇关于转换(解码)十六进制字符串为ASCII或任何其他可理解的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆