CRCMOD python3多项式错误 [英] Crcmod python3 polynomial error

查看:256
本文介绍了CRCMOD python3多项式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在python3程序中使用crc校验和,但是我对crc的了解几乎不存在。

I need to use a crc checksum in a python3 program, but my knowledge of crc is virtually non-existent.

这是我编写的测试代码

import crcmod

crc_func = crcmod.mkCrcFun(0x1d, initCrc=0x07, xorOut=0x00)
print(hex(crc_func(b'123456789')))

运行此命令时出现以下错误:

When I run this, I get the following error:

ValueError: The degree of the polynomial must be 8, 16, 24, 32 or 64

但是1D是8位,所以我一定做错了。
请说明我做错了。

But 1D is 8 bit, so I must be doing something wrong. Please explain what I did wrong.

推荐答案


但是1D是8位

But 1D is 8 bit

不,不是;它的5位:

>>> bin(0x1d)
'0b11101'

此模块定义事物的方式(请参见 _verifyPoly 函数)四舍五入,因此算作 4位多项式。在此, 8位多项式表示为 1。必须在 0x100 0x1ff (含)之间。当然,该范围内的大多数多项式都不会给出有用的结果,但是至少可以由该模块处理。

The way this module defines things (see the _verifyPoly function) rounds down, so this counts as a "4-bit polynomial". An "8-bit polynomial" has to be between 0x100 and 0x1ff (inclusive). Of course most of the polynomials in that range will not give useful results, but they can at least be handled by this module.


请说明我做错了。

Please explain what I did wrong.

文档说:


此整数中的位是多项式的系数。允许的唯一多项式是那些生成8、16、24、32或64位CRC的多项式。

The bits in this integer are the coefficients of the polynomial. The only polynomials allowed are those that generate 8, 16, 24, 32, or 64 bit CRCs.

0x1d 不会生成8度多项式。

0x1d does not generate an 8-degree polynomial.

如果这些都不对您有意义,那么文档明确说,就在顶部:

If none of this makes any sense to you, well, the docs explicitly say, right at the top:


此软件包中没有尝试解释CRC的工作原理……

There is no attempt in this package to explain how the CRC works…

由您决定在应用程序中使用哪些多项式。在crcmod.predefined中预定义了一些常见的CRC算法。如果没有指定要使用的多项式,则需要进行一些研究以找到适合您的应用的多项式。单元测试脚本test.py中提供了示例。

It is up to you to decide what polynomials to use in your application. Some common CRC algorithms are predefined in crcmod.predefined. If someone has not specified the polynomials to use, you will need to do some research to find one suitable for your application. Examples are available in the unit test script test.py.

如果您不想学习CRC的工作原理,并想出如何设计和编码,一个适合自己的多项式,只需使用预定义的之一即可。

If you don't want to learn how CRC works and figure out how to design and encode an appropriate polynomial yourself, just use one of the predefined ones.

更一般地,如果您无需考虑特定的CRC多项式,甚至不了解这意味着什么,您可能没有理由首先使用此模块。如果您只是需要使用crc校验和,则stdlib中已经有一个非常好的CRC函数, zlib.crc32

More generally, if you don't have a specific CRC polynomial in mind, or don't even understand what this means, you probably have no reason to use this module in the first place. If you just "need to use a crc checksum", there's already a perfectly good CRC function in the stdlib, zlib.crc32.

为此,如果没有要实际上成为一个CRC,只是一个相当可靠的校验和,您可能想要 zlib.adler32 代替。

For that matter, if it doesn't have to actually be a CRC, just a reasonably reliable checksum, you probably want zlib.adler32 instead.

或者,如果 adler32 crc32 出于某些原因是不够的,不管是什么原因,我敢打赌您实际上不需要校验和,但需要真正的哈希,而且更好的CRC多项式不能帮助您。您需要一个不同的算法,可能是 hashlib

Or, if adler32 and crc32 aren't sufficient for some reason, whatever that reason is, I'd wager you don't actually need a checksum but a real hash, and a better CRC polynomial isn't gong to help you; you want a different algorithm, probably something out of hashlib.

这篇关于CRCMOD python3多项式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆