9

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

bky
  • 1,314
  • 3
  • 11
  • 14
  • Are you sure about that first hex string? I don't see why it would start with so many 0s. – Alex Hall Jul 02 '17 at 12:31
  • 3
    Your expected output is only the end of the real output. So, you should have some rule to cut out the part you need. – Thierry Lathuille Jul 02 '17 at 12:34
  • 1
    Well - that's the correct output for the given input... I'll just point out that `binascii.hexlify(base64.b64decode(itemid)).upper()` is more straight-forward – Jon Clements Jul 02 '17 at 12:51
  • @ThierryLathuille wow, you're right ... why I didn't saw that, but why do I have so much leading chars which are unnecessary? – bky Jul 02 '17 at 12:58
  • @JonClements thank you so much, this is really a very awesome straight-forward codeline – bky Jul 02 '17 at 12:59

2 Answers2

29

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'
Filip Kilibarda
  • 2,484
  • 2
  • 20
  • 31
IvanD
  • 7,971
  • 4
  • 35
  • 33
-2

If you just take the first few bytes from the expected output hex and convert them:

import base64
base64.b64encode('\x00\x00\x00\x00\x19\x4B\xD6')

you get:

AAAAABlL1g==

which doesn't match the beginning of your input.

Anthon
  • 69,918
  • 32
  • 186
  • 246