2017-08-04 40 views
0

我想根據另一個key來計算第10輪的key,如果ikk是一個字符串,那麼如何將它轉換爲字符串呢?如何將一組十六進制數字作爲字符串?

def test(): 
    ##### ikk= '000102030405060708090a0b0c0d0e0f' ===> It works!!! 
    ikk= 000102030405060708090a0b0c0d0e0f 
    ik=str(ikk) 
    data = [re.findall('..', item) for item in key.split('\n')] 
    for item in data: 
     if item: 
      result = [int(x, 16) for x in item] 
      print(result) 
      result = keyScheduleRounds(result, 0, 10) 
      tt=''.join(["%02x"%d for d in result]) 
      print(tt) 

錯誤,我發現:

ik= str(000102030405060708090a0b0c0d0e0f)       ^
SyntaxError: invalid token 
+0

看起來'000102030405060708090a0b0c0d0e0f'不是一個有效的數字。 – taras

+0

@taras它是我的鑰匙,它是正確的我只需要把它作爲字符串:'000102030405060708090a0b0c0d0e0f',這是我的問題。 – tierrytestu

+0

不,你可以用它作爲一個十六進制,如果你告訴Python那是一個十六進制。像這樣:ikk = 0x000102030405060708090a0b0c0d0e0f – BoboDarph

回答

1

如果IKK是一個十六進制數字(和它看起來像),試試這個方法:ikk = 0x000102030405060708090a0b0c0d0e0f。注意0x在開始時會告訴python這是一個十六進制數字,而不是十進制數字。

+0

它給了我這個錯誤:data = [key.split('\ n')中item的[re.findall('..',item)] AttributeError:'long'對象沒有屬性'split' – tierrytestu

+1

因爲您試圖在非字符串對象上使用split函數。 – BoboDarph

+0

@tierrytestu你想用這個十六進制數來完成什麼? –

1

集IK:

ik= bytearray.fromhex("000102030405060708090a0b0c0d0e0f").decode() 

這將十六進制轉換爲字符串。完整的代碼寫在下面。

def test(): 
    ik = bytearray.fromhex("000102030405060708090a0b0c0d0e0f").decode() 
    data = [re.findall('..', item) for item in key.split('\n')] 
    for item in data: 
     if item: 
      result = [int(x, 16) for x in item] 
      print(result) 
      result = keyScheduleRounds(result, 0, 10) 
      tt=''.join(["%02x"%d for d in result]) 
      print(tt) 
+0

它給了我這個錯誤:UnicodeEncodeError:'十進制'編解碼器無法編碼字符u'\ x00'在位置0:無效十進制Unicode字符串 >>> – tierrytestu

相關問題