CRC32 python实现

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值