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

5
  • Are you sure about that first hex string? I don't see why it would start with so many 0s. Commented Jul 2, 2017 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. Commented Jul 2, 2017 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 Commented Jul 2, 2017 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? Commented Jul 2, 2017 at 12:58
  • @JonClements thank you so much, this is really a very awesome straight-forward codeline Commented Jul 2, 2017 at 12:59

3 Answers 3

32

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'
Sign up to request clarification or add additional context in comments.

Comments

0

For others who wants prefix such as \x you can try the following code:

base64_response = "aGVsbG8h"
base64_decoded_response = base64.b64decode(base64_response) 
print(''.join([rf'\x{byte:02x}' for byte in base64_decoded_response])) 
'\x68\x65\x6c\x6c\x6f\x21' 

Comments

-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.