2017-08-29 752 views
0

我需要python中hexlify的一點幫助。
我想要用戶粘貼十六進制數據,如:b'EncryptedD4t4',它將是unhex。
我想這樣的:在python中unhexlify輸入

import binascii as b 
para1 = str(input()) 
b.unhexlify(bytes(para1,encoding='utf-8')) 

當我運行它:

b'6578616d706c65' 
Traceback (most recent call last): 
    File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\filenme.py", line 3, in <module> 
    b.unhexlify(bytes(para1,encoding='utf-8')) 
binascii.Error: Odd-length string 

但是,當我寫不b',最後',它的工作原理。
我想讓用戶直接輸入b''

+0

你爲什麼要用戶添加呢?如果他們必須的話,爲什麼不去分割'[2:-1]'來得到除了那些首尾字符之外的所有內容? – jonrsharpe

+0

@jonrsharpe謝謝,我試過'[2:]'但我不知道如何切片最後的報價。 – user8533214

回答

0

你需要從字符串周圍去掉b'',一個簡單的方法來做到這一點是切片: b.unhexlify(bytes(para1[2:-1],encoding='utf-8'))