Reputation: 51
I got some log files from a search engine. The following is supposed to be country names but I failed to converted it to ASCII in Python. Any help will be appreciated.
Data: 505a44facaa3e758845f6e101f4e21f9d99acf63
Code:
ascii_string = str(base64.b16decode(hex_data))[2:-1]
Error: Non-base16 digit found
Upvotes: 0
Views: 458
Reputation: 180441
If you want to decode the hex from the Data string:
s = "Data: 505a44facaa3e758845f6e101f4e21f9d99acf63"
print( s.split()[1].decode("hex")
For python3 use unhexlify:
print(binascii.unhexlify(s.split()[1]))
But neither will return a country name.
Your string is actually "GB"
sha1 hashed.
In [9]: import sha
In [10]: sha.new("GB").hexdigest()
Out[10]: '505a44facaa3e758845f6e101f4e21f9d99acf63'
Upvotes: 1
Reputation: 87084
Are you sure that 505a44facaa3e758845f6e101f4e21f9d99acf63
represents an encoded country? It looks suspiciously like the hex digest of a SHA1 hash.
hex_data
implies that the data is hex encoded. It can be decoded like this:
>>> hex_data = '505a44facaa3e758845f6e101f4e21f9d99acf63'
>>> hex_data.decode('hex')
'PZD\xfa\xca\xa3\xe7X\x84_n\x10\x1fN!\xf9\xd9\x9a\xcfc'
Where to go from there is anyone's guess.
Upvotes: 1
Reputation: 15433
I think that you just need to convert hex_data
to upper case:
ascii_string = str(base64.b16decode(hex_data.upper()))[2:-1]
Upvotes: -1