资源简介

crc32及crc32-mpeg算法,主要用查表法实现,本人自己编写,实测么有问题

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRCMethod
{
    class CRCMethod
    {
        protected UInt32[] Crc32Table;
        protected UInt32[] Crc32_MEPGTable;

        //生成CRC-32码表
        public void GetCRC32Table()
        {
            UInt32 Crc;
            Crc32Table = new UInt32[256];
            int i j;
            for (i = 0; i < 256; i++)
            {
                Crc = (UInt32)i;
                for (j = 8; j > 0; j--)
                {
                    if ((Crc & 1) == 1)
                    {
                        Crc = (Crc >> 1) ^ 0xEDB88320;
                    }
                    else
                    {
                        Crc >>= 1;
                    }
                }
                Crc32Table[i] = Crc;
                Console.WriteLine(“crc32:“ +i+“:“+String.Format(“{0:X8}“ Crc32Table[i]));
            }
        }

        //生成CRC-32/MPEG-2码表
        public void GetCRC32_MPEGTable()
        {
            UInt32 Crc;
            Crc32_MEPGTable = new UInt32[256];
            int i j;
            for (i = 0; i < 256; i++)
            {
                Crc = (UInt32)i<<24;
                for (j = 8; j > 0; j--)
                {
                    if ((Crc & 0x80000000) != 0)
                    {
                        Crc = (Crc << 1) ^ 0x04c11db7;
                    }
                    else
                    {
                        Crc = Crc << 1;
                    }
                }
                Crc32_MEPGTable[i] = Crc;
                Console.WriteLine(“crc32_mepg“ +i+“:“+String.Format(“{0:X8}“ Crc32_MEPGTable[i]));
            }
        }

        //获取字符串的CRC-32校验值
        public ulong GetCRC32Str(string sInputString)
        {
            //生成码表
            GetCRC32Table();
            byte[] buffer = System.Text.Encoding.Default.GetBytes(sInputString);
            UInt32 value = 0xffffffff;
            int len = buffer.Length;
            for (int i = 0; i < len; i++)
            {
                value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]];
            }
            return value ^ 0xffffffff;
        }

        //获取字符串的CRC-32/MPEG-2校验值
        public ulong GetCRC32_MPEGStr(string sInputString)
        {
           // 生成码表
            GetCRC32_MPEGTable();
            byte[] buffer = System.Text.Encoding.Default.GetBytes(sInputString);
            UInt32 value = 0xffffffff;
            int len = buffer.Length;
            for (int i = 0; i < len; i++)
            {
                value = (value << 8) ^ Crc32_MEPGTable[(value >> 24) ^ buffer[i]];
            }
            return value;         
        }

        public string StringToHexString(string s)
        {
            byte[] b = System.Text.Encoding.Default.GetBytes(s);//按照指定编码将string编程字节数组
            string result = string.Empty;
            for (in

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-09-13 16:09  CRCMethod\
     目录           0  2018-09-14 11:52  CRCMethod\CRCMethod\
     文件        3291  2018-09-17 16:52  CRCMethod\CRCMethod\CRCMethod.cs
     文件        2479  2018-09-14 13:23  CRCMethod\CRCMethod\CRCMethod.csproj
     文件         532  2018-09-17 16:07  CRCMethod\CRCMethod\Program.cs
     目录           0  2018-09-13 16:09  CRCMethod\CRCMethod\Properties\
     文件        1368  2018-09-13 16:09  CRCMethod\CRCMethod\Properties\AssemblyInfo.cs
     目录           0  2018-09-14 11:52  CRCMethod\CRCMethod\bin\
     目录           0  2018-09-13 16:14  CRCMethod\CRCMethod\bin\Debug\
     文件        6144  2018-09-17 16:51  CRCMethod\CRCMethod\bin\Debug\CRCMethod.exe
     文件       17920  2018-09-17 16:51  CRCMethod\CRCMethod\bin\Debug\CRCMethod.pdb
     文件       11600  2018-09-17 16:51  CRCMethod\CRCMethod\bin\Debug\CRCMethod.vshost.exe
     文件         490  2010-03-17 22:39  CRCMethod\CRCMethod\bin\Debug\CRCMethod.vshost.exe.manifest
     目录           0  2018-09-18 09:24  CRCMethod\CRCMethod\bin\Release\
     目录           0  2018-09-13 16:09  CRCMethod\CRCMethod\obj\
     目录           0  2018-09-13 16:09  CRCMethod\CRCMethod\obj\x86\
     目录           0  2018-09-17 16:51  CRCMethod\CRCMethod\obj\x86\Debug\
     文件         319  2018-09-17 16:51  CRCMethod\CRCMethod\obj\x86\Debug\CRCMethod.csproj.FileListAbsolute.txt
     文件        6144  2018-09-17 16:51  CRCMethod\CRCMethod\obj\x86\Debug\CRCMethod.exe
     文件       17920  2018-09-17 16:51  CRCMethod\CRCMethod\obj\x86\Debug\CRCMethod.pdb
     文件        5833  2018-09-17 16:51  CRCMethod\CRCMethod\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件        6016  2018-09-14 09:50  CRCMethod\CRCMethod\obj\x86\Debug\ResolveAssemblyReference.cache
     目录           0  2018-09-18 09:24  CRCMethod\CRCMethod\obj\x86\Debug\TempPE\
     文件         869  2018-09-13 16:09  CRCMethod\CRCMethod.sln
     文件       18432  2018-09-17 17:02  CRCMethod\CRCMethod.suo

评论

共有 条评论