def hex2vec(h, ell):
"""hex2vec(h, ell) generates sign vector of length ell from the hex string h.
ell must be <= 4*len(h) (excluding the optional leading "0x")
"""
if h[0:2] in ['0x', '0X']:
h = h[2:]
nybble = numpy.array([
[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 1],
[0, 1, 0, 0], [0, 1, 0, 1], [
0, 1, 1, 0], [0, 1, 1, 1],
[1, 0, 0, 0], [1, 0, 0, 1], [
1, 0, 1, 0], [1, 0, 1, 1],
[1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]])
vec = numpy.ravel(numpy.array([nybble[int(x, 16)] for x in h]))
if len(vec) < ell:
raise ValueError('hex string too short')
return vec[len(vec) - ell:]
评论列表
文章目录