• C# CRC


    CRC校验位

    using System; static class Program { static void Main() { string input = "8000"; var bytes = HexToBytes(input); string hex = Crc16.ComputeChecksum(bytes).ToString("x2"); Console.WriteLine(hex); //c061 } static byte[] HexToBytes(string input) { byte[] result = new byte[input.Length / 2]; for(int i = 0; i < result.Length; i++) { result[i] = Convert.ToByte(input.Substring(2 * i, 2), 16); } return result; } public static class Crc16 { const ushort polynomial = 0xA001; static readonly ushort[] table = new ushort[256]; public static ushort ComputeChecksum(byte[] bytes) { ushort crc = 0; for (int i = 0; i < bytes.Length; ++i) { byte index = (byte)(crc ^ bytes[i]); crc = (ushort)((crc >> 8) ^ table[index]); } return crc; } static Crc16() { ushort value; ushort temp; for (ushort i = 0; i < table.Length; ++i) { value = 0; temp = i; for (byte j = 0; j < 8; ++j) { if (((value ^ temp) & 0x0001) != 0) { value = (ushort)((value >> 1) ^ polynomial); } else { value >>= 1; } temp >>= 1; } table[i] = value; } } } }
  • 相关阅读:
    2018级软件秋季总结
    对我影响最大的三位老师
    自我介绍
    Js中的一个日期处理格式化函数
    SQL update语句加减乘除运算
    用css让一个容器水平垂直
    position:absolute 的深入探讨
    java正则表达式
    Session
    使用Cookie进行会话管理
  • 原文地址:https://www.cnblogs.com/z45281625/p/10763837.html
Copyright © 2020-2023  润新知