2012-09-17 167 views
0

我想將一個字符串編碼變量(最初是一個十六進制guid)轉換爲十六進制guid並回到相同的字符串編碼版本。轉換爲base64字符串編碼,反之亦然

我做Python標準庫

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

上面代碼中的這個使用Base64包完成的任務。現在我需要將變量cguid轉換回guid。

我該如何完成任務?

我試過切換解碼和編碼,但它似乎不工作。它向我顯示了一個錯誤。

>>> 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 
>>> 

回答

2

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

+0

沒意識到,我必須把它在十六進制解碼之前,我可以編碼。謝謝Marcus! – ronak

相關問題