如何通过 Python 将日志文件中的数据转换为 ASCII 字符串?
how to convert data in a log file to ASCII string by Python?
我从搜索引擎获得了一些日志文件。以下应该是国家名称,但我未能在 Python 中将其转换为 ASCII。任何帮助将不胜感激。
Data: 505a44facaa3e758845f6e101f4e21f9d99acf63
代码:
ascii_string = str(base64.b16decode(hex_data))[2:-1]
错误:Non-base16 digit found
我认为你只需要将 hex_data
转换为大写:
ascii_string = str(base64.b16decode(hex_data.upper()))[2:-1]
如果要从数据字符串中解码十六进制:
s = "Data: 505a44facaa3e758845f6e101f4e21f9d99acf63"
print( s.split()[1].decode("hex")
对于 python3 使用 unhexlify:
print(binascii.unhexlify(s.split()[1]))
但return国家名称也不会。
您的字符串实际上是 "GB"
sha1 哈希。
In [9]: import sha
In [10]: sha.new("GB").hexdigest()
Out[10]: '505a44facaa3e758845f6e101f4e21f9d99acf63'
您确定 505a44facaa3e758845f6e101f4e21f9d99acf63
代表编码国家吗?它看起来很像 SHA1 散列的十六进制摘要。
hex_data
表示数据是十六进制编码的。可以这样解码:
>>> hex_data = '505a44facaa3e758845f6e101f4e21f9d99acf63'
>>> hex_data.decode('hex')
'PZD\xfa\xca\xa3\xe7X\x84_n\x10\x1fN!\xf9\xd9\x9a\xcfc'
从那里去哪里是任何人的猜测。
我从搜索引擎获得了一些日志文件。以下应该是国家名称,但我未能在 Python 中将其转换为 ASCII。任何帮助将不胜感激。
Data: 505a44facaa3e758845f6e101f4e21f9d99acf63
代码:
ascii_string = str(base64.b16decode(hex_data))[2:-1]
错误:Non-base16 digit found
我认为你只需要将 hex_data
转换为大写:
ascii_string = str(base64.b16decode(hex_data.upper()))[2:-1]
如果要从数据字符串中解码十六进制:
s = "Data: 505a44facaa3e758845f6e101f4e21f9d99acf63"
print( s.split()[1].decode("hex")
对于 python3 使用 unhexlify:
print(binascii.unhexlify(s.split()[1]))
但return国家名称也不会。
您的字符串实际上是 "GB"
sha1 哈希。
In [9]: import sha
In [10]: sha.new("GB").hexdigest()
Out[10]: '505a44facaa3e758845f6e101f4e21f9d99acf63'
您确定 505a44facaa3e758845f6e101f4e21f9d99acf63
代表编码国家吗?它看起来很像 SHA1 散列的十六进制摘要。
hex_data
表示数据是十六进制编码的。可以这样解码:
>>> hex_data = '505a44facaa3e758845f6e101f4e21f9d99acf63'
>>> hex_data.decode('hex')
'PZD\xfa\xca\xa3\xe7X\x84_n\x10\x1fN!\xf9\xd9\x9a\xcfc'
从那里去哪里是任何人的猜测。