import crcmod
import numpy
def byte_swap(s):
s = [s[6:8], s[4:6], s[2:4], s[0:2]]
return "".join(s)
def data2npy(data, shape, swap_byte=False):
if shape < 0:
shape = len(data) * 4
shape_num = numpy.prod(shape)
data_num = len(data) * 4
if data_num < shape_num:
raise Exception("Saving npy, num data %d < num shape %d" %
(data_num, shape_num))
# preserve only valid data
data_num = (shape_num / 4) + (shape_num % 4 > 0)
data_num = int(data_num)
data = data[:data_num]
if swap_byte:
data = [byte_swap(x) for x in data]
data = "".join(data)
data = bytes.fromhex(data)
np_dt = numpy.dtype(numpy.int8)
np_data = numpy.frombuffer(data, np_dt)
np_data.reshape(shape)
return np_data
def generate_data_crc(data):
npy_data = data2npy(data, shape=-1, swap_byte=True)
crc32_func = crcmod.mkCrcFun(0x104C11DB7,initCrc=0xffffffff,rev=True,xorOut=0)
crc32 = crc32_func(npy_data.tobytes())
return crc32
#用法测试
data_origin = ["00800806","00800000","00000000","00000080","00000000","00000000","00000000","000c0000","00420003"]
crc32_out = generate_data_crc(data_origin)
print(hex(crc32_out))