我有这样的帧构建: {0x00,0x00,0x00,0x00,0x00} 。
C#脚本计算 crc8
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
},
},
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
})
]
有人可以告诉我如何在Python中正确编写它吗?
答案 0 :(得分:2)
您链接的网站似乎使用与您发布的网站相同的算法。转换到Python很容易,所有比特的代码都保持不变,你需要改变的只是循环输入缓冲区的代码。
def crc8(buff):
crc = 0
for b in buff:
crc ^= b
for i in range(8):
if crc & 1:
crc = (crc >> 1) ^ 0x8C
else:
crc >>= 1
return crc
buff = [0x12, 0xAB, 0x34]
crc = crc8(buff)
print(hex(crc))
<强>输出强>
0x17
如果buff
是bytes
或bytearray
对象,此代码也能正常运行。