2017-07-31 121 views
1

我試圖解碼類似以下格式十六進制字符串:如何解碼(非常大的值)十六進制字符串爲十進制?

0x00000000000000000000000000000000000000000000000bf97e2a21966df7fe 

我能夠將它與online calculator解碼。正確的解碼數字應該是220892037897060743166

然而,當我試圖把它與下面的代碼蟒蛇解碼,則返回錯誤:

"0x00000000000000000000000000000000000000000000000bf97e2a21966df7fe".decode("hex") 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-32-1cf86ff46cbc> in <module>() 
     9 key=keyarr[0] 
    10 
---> 11 "0x00000000000000000000000000000000000000000000000bf97e2a21966df7fe".decode("hex") 

/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/hex_codec.py in hex_decode(input, errors) 
    40  """ 
    41  assert errors == 'strict' 
---> 42  output = binascii.a2b_hex(input) 
    43  return (output, len(input)) 
    44 

TypeError: Non-hexadecimal digit found 

然後我刪除了0X的十六進制數的前面,又試了一次:

"00000000000000000000000000000000000000000000000bf97e2a21966df7fe".decode("hex") 

然後輸出成爲:

'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\xf9~*!\x96m\xf7\xfe' 

我其實不明白輸出..

如果你想知道這些數字來自哪裏,那麼它們是Ethereum blockchain (ERC20) tokens

回答

6

調用它int以16爲基礎:

int(your_string, base=16) 

.decode('hex')意味着你要正確對待字符串作爲單個字符的十六進制編碼的序列。

0

與Python 3.6.1

>>> a = '0x00000000000000000000000000000000000000000000000bf97e2a21966df7fe' 
>>> a 
'0x00000000000000000000000000000000000000000000000bf97e2a21966df7fe' 
>>> int(a, 16) 
220892037897060743166 
相關問題