猿问

Is there CRC32 api in C#/.NET?

Java version:

public static long crc32(byte[] data) {
    CRC32 crc32 = new CRC32();
    crc32.update(data);    
    return crc32.getValue();
}

How to implement it simplely in C#/.NET?


小怪兽爱吃肉
浏览 521回答 2
2回答

qq_笑_17

貌似没有官方实现,百度两个吧

Helenr

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace wcs_csharp_sdk.main.util{&nbsp; &nbsp; public class CRC32&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static protected ulong[] Crc32Table;&nbsp; &nbsp; &nbsp; &nbsp; //生成CRC32码表&nbsp; &nbsp; &nbsp; &nbsp; static public void GetCRC32Table()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ulong Crc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Crc32Table = new ulong[256];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i, j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < 256; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Crc = (ulong)i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (j = 8; j > 0; j--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((Crc & 1) == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Crc = (Crc >> 1) ^ 0xEDB88320;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Crc >>= 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Crc32Table[i] = Crc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //获取byte[]的CRC32校验值&nbsp; &nbsp; &nbsp; &nbsp; static public ulong GetCRC32(byte[] buffer)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //生成码表&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCRC32Table();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ulong value = 0xffffffff;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int len = buffer.Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < len; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value ^ 0xffffffff;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //获取字符串的CRC32校验值&nbsp; &nbsp; &nbsp; &nbsp; static public ulong GetCRC32(string sInputString)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //生成码表&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetCRC32Table();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(sInputString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ulong value = 0xffffffff;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int len = buffer.Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < len; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value ^ 0xffffffff;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答