2021-02-03 20:55:39 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
re_hex = r'^[0-9a-fA-Z]+$'
|
|
|
|
def is_hex(hx):
|
|
|
|
m = re.match(re_hex, hx)
|
|
|
|
if m == None:
|
|
|
|
raise ValueError('not valid hex {}'.format(hx))
|
|
|
|
|
|
|
|
return hx
|
|
|
|
|
|
|
|
|
|
|
|
def strip_0x(hx):
|
|
|
|
if len(hx) >= 2 and hx[:2] == '0x':
|
|
|
|
hx = hx[2:]
|
|
|
|
return is_hex(hx)
|
|
|
|
|
|
|
|
|
2021-02-03 23:03:39 +01:00
|
|
|
def with_0x(hx):
|
|
|
|
if len(hx) >= 2 and hx[:2] == '0x':
|
|
|
|
hx = hx[2:]
|
|
|
|
return '0x' + is_hex(hx)
|
|
|
|
|
|
|
|
|
2021-02-03 20:55:39 +01:00
|
|
|
def hex_to_int(hx, endianness='big'):
|
|
|
|
hx = strip_0x(hx)
|
|
|
|
b = bytes.fromhex(hx)
|
|
|
|
return int.from_bytes(b, endianness)
|