C# CRC32校验 之 CRC-32/MPEG-2算法

先引用几条链接:

1.CRC32的几种分类:http://www.ip33.com/crc.html

2.CRC32算法在C#中的一种实现方法“:https://www.cnblogs.com/Kconnie/p/3538194.html

3.全套CRC校验 C的实现

4.C#  CRC32校验 之 CRC-32/MPEG-2算法的实现:

 //1EA90DFF
    string s = "E4BFA10813100000";

    /// 
    /// 进行CRC32验证
    /// 
    /// 
    /// 
    public static string CRC32_MPEG_2_Str(string str)
    {
        uint[] CRC32_Data;
        CRC32_Data = strToToHexByte(str);
        UInt32 CRC32_Result = 0;
        CRC32_Result = CRC32_MPEG_2(CRC32_Data, CRC32_Data.Length);
        string temp=(CRC32_Result.ToString("X4"));
        temp = temp.Length == 8 ? temp : "0" + temp;
       return   temp;
    }
    private static uint[] strToToHexByte(string hexString)
    {
        hexString = hexString.Replace(" ", "");
        if ((hexString.Length % 2) != 0)
            hexString += " ";
        uint[] returnBytes = new uint[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        return returnBytes;
    }

    ///  
    /// 获取文件的CRC32标识 
    ///  
    ///  
    ///  
    private  static UInt32 CRC32_MPEG_2(uint[] data, int length)
    {
        uint i;
        UInt32 crc = 0xffffffff, j = 0;

        while ((length--) != 0)
        {
            crc ^= (UInt32)data[j] << 24;
            j++;
            for (i = 0; i < 8; ++i)
            {
                if ((crc & 0x80000000) != 0)
                    crc = (crc << 1) ^ 0x04C11DB7;
                else
                    crc <<= 1;
            }
        }
        return crc;
    }

 

你可能感兴趣的:(C# CRC32校验 之 CRC-32/MPEG-2算法)