是否有一个简单的算法从lua中的一个表创建一个crc8校验和?多项式应该是x^8+x^5+x^4+1 (0x31),这个算法将用于检查DS28CM00 UID芯片的UID。在这里,您可以找到由芯片返回的表(LS-字节最后一次):
table = {112,232,9,80,1,0,0}谢谢你的帮助
发布于 2021-07-07 09:17:42
对于Lua 5.3+
local function crc8(t)
local c = 0
for _, b in ipairs(t) do
for i = 0, 7 do
c = c >> 1 ~ ((c ~ b >> i) & 1) * 0x8C
end
end
return c
end
print(crc8{112, 232, 9, 80, 1, 0, 0}) --> 219
print(crc8{2, 0x1C, 0xB8, 1, 0, 0, 0}) --> 0xA2 as in example from AN-27对于Lua 5.2-
local function crc8(t)
local c = 0
for _, b in ipairs(t) do
for i = 0, 7 do
local c0 = c % 2
local b0 = b % 2
c = (c - c0) / 2
b = (b - b0) / 2
if c0 + b0 == 1 then
c = c + 0x80 + (c % 16 < 8 and 8 or -8) + (c % 8 < 4 and 4 or -4)
end
end
end
return c
endhttps://stackoverflow.com/questions/68280849
复制相似问题