Python从c#转换从4/5字节帧计算CRC8

时间:2018-06-15 10:54:04

标签: c# python c crc

我有这样的帧构建: {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中正确编写它吗?

1 个答案:

答案 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

如果buffbytesbytearray对象,此代码也能正常运行。