python base64 to hex

20,741

The best way for converting base64 to hex string is:

# Python 2
>>> base64.b64decode('woidjw==').encode('hex')
# Python 3
>>> base64.b64decode('woidjw==').hex()
'c2889d8f'

You can also try it just like this:

>>> base64.b64decode('woidjw==')

but I am not a fan of the output:

'\xc2\x88\x9d\x8f'

As far as your original request goes, there must be something wrong with your initial data, as it does not result in data that you expected:

>>> base64.b64decode('AAMkADk0ZjU4ODc1LTY1MzAtNDdhZS04NGU5LTAwYjE2Mzg5NDA1ZABGAAAAAAAZS9Y2rt6uTJgnyUZSiNf0BwC6iam6EuExS4FgbbOF87exAAAAdGVuAAC6iam6EuExS4FgbbOF87exAAAxj5dhAAA=').encode('hex')

'0003240039346635383837352d363533302d343761652d383465392d30306231363338393430356400460000000000194bd636aedeae4c9827c9465288d7f40700ba89a9ba12e1314b81606db385f3b7b100000074656e0000ba89a9ba12e1314b81606db385f3b7b10000318f97610000'
Share:
20,741
ben
Author by

ben

Updated on February 03, 2020

Comments

  • ben
    ben over 4 years

    Since two weeks, I'm trying and reading to solve this problem, but everything I tried didn't worked :-(

    I'm using python 2.7.

    I do have, as far as I understand, a base64-string from the format: AAMkADk0ZjU4ODc1LTY1MzAtNDdhZS04NGU5LTAwYjE2Mzg5NDA1ZABGAAAAAAAZS9Y2rt6uTJgnyUZSiNf0BwC6iam6EuExS4FgbbOF87exAAAAdGVuAAC6iam6EuExS4FgbbOF87exAAAxj5dhAAA=

    I want to convert it to a hex-string. Which should result in 00000000194BD636AEDEAE4C9827C9465288D7F40700BA89A9BA12E1314B81606DB385F3B7B100000074656E0000BA89A9BA12E1314B81606DB385F3B7B10000318F97610000

    I tried it with the following code:

    def itemid_to_entryid(itemid):
        decoded_val = base64.b64decode(itemid)
        decoded_val = ''.join( ["%02X" % ord(x) for x in decoded_val ] ).strip()
        decoded_val = decoded_val.upper()
        return decoded_val
    
    
    itemid = 'AAMkADk0ZjU4ODc1LTY1MzAtNDdhZS04NGU5LTAwYjE2Mzg5NDA1ZABGAAAAAAAZS9Y2rt6uTJgnyUZSiNf0BwC6iam6EuExS4FgbbOF87exAAAAdGVuAAC6iam6EuExS4FgbbOF87exAAAxj5dhAAA='
    
    entryid = itemid_to_entryid(itemid)
    print(entryid)
    

    which always returns me the following: 0003240039346635383837352D363533302D343761652D383465392D30306231363338393430356400460000000000194BD636AEDEAE4C9827C9465288D7F40700BA89A9BA12E1314B81606DB385F3B7B100000074656E0000BA89A9BA12E1314B81606DB385F3B7B10000318F97610000

    and I really don't get, what I'm doing wrong and really would appreciate any help in understanding what I'm doing wrong.

    Kind regards Ben