0

I am trying to convert a string encoded variable (which originally is a hex guid) into the hex guid and back to the same string encoded version.

I am doing this using base64 package of the python standard library

>>> import base64
>>> guid
'Dw8OAwQFBgcIBQABAgMEBw==\n'
>>> cguid=base64.decodestring(guid).encode('hex')
'0f0f0e03040506070805000102030407'

Above code accomplishes the task. Now I need to convert the variable cguid back to guid.

How do I accomplish the task?

I have tried switching the decode and encode but it does not seem to work. It shows me an error.

>>> base64.encodestring(cguid).decode('hex')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/nb/python/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Odd-length string
>>> 

1 Answer 1

2

base64.encodestring(cguid).decode('hex') => base64.encodestring(cguid.decode('hex'))

Sign up to request clarification or add additional context in comments.

1 Comment

Didnt realize that I had to decode it in hex before I could encode it. Thanks Marcus!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.