2017-07-31 72 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 
相关问题