我这个可以用
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace CRC
{
/// <summary>
/// CRC32校验
/// </summary>
public sealed class CRC32
{
static CRC32()
{
GetCRC32Table();
} private static ulong[] Crc32Table;
/// <summary>
/// 生成CRC32码表
/// </summary>
private static void GetCRC32Table()
{
ulong Crc;
Crc32Table = new ulong[256];
int i,j;
for(i = 0;i < 256; i++)
{
Crc = (ulong)i;
for (j = 8; j > 0; j--)
{
if ((Crc & 1) == 1)
Crc = (Crc >> 1) ^ 0xEDB88320;
else
Crc >>= 1;
}
Crc32Table[i] = Crc;
}
} /// <summary>
/// 字节数组校验
/// </summary>
/// <param name="buffer">ref 字节数组</param>
/// <returns></returns>
public static ulong ByteCRC(ref byte[] buffer)
{
ulong value = 0xffffffff;
ulong len = (ulong)buffer.Length;
for (ulong i = 0; i < len; i++)
{
value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]];
}
return value ^ 0xffffffff;
} /// <summary>
/// 字符串校验
/// </summary>
/// <param name="sInputValue">字符串</param>
/// <returns></returns>
public static ulong StringCRC(string sInputValue)
{
byte[] buffer = Encoding.ASCII.GetBytes(sInputValue);
return ByteCRC(ref buffer);
} /// <summary>
/// 文件校验
/// </summary>
/// <param name="sInputFilename">文件名</param>
/// <returns></returns>
public static ulong FileCRC(string sInputFileName)
{
FileStream inFile = new System.IO.FileStream(sInputFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] bInput = new byte[inFile.Length];
inFile.Read(bInput, 0, bInput.Length);
inFile.Close(); return ByteCRC(ref bInput);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace CRC
{
/// <summary>
/// CRC32校验
/// </summary>
public sealed class CRC32
{
static CRC32()
{
GetCRC32Table();
} private static ulong[] Crc32Table;
/// <summary>
/// 生成CRC32码表
/// </summary>
private static void GetCRC32Table()
{
ulong Crc;
Crc32Table = new ulong[256];
int i,j;
for(i = 0;i < 256; i++)
{
Crc = (ulong)i;
for (j = 8; j > 0; j--)
{
if ((Crc & 1) == 1)
Crc = (Crc >> 1) ^ 0xEDB88320;
else
Crc >>= 1;
}
Crc32Table[i] = Crc;
}
} /// <summary>
/// 字节数组校验
/// </summary>
/// <param name="buffer">ref 字节数组</param>
/// <returns></returns>
public static ulong ByteCRC(ref byte[] buffer)
{
ulong value = 0xffffffff;
ulong len = (ulong)buffer.Length;
for (ulong i = 0; i < len; i++)
{
value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]];
}
return value ^ 0xffffffff;
} /// <summary>
/// 字符串校验
/// </summary>
/// <param name="sInputValue">字符串</param>
/// <returns></returns>
public static ulong StringCRC(string sInputValue)
{
byte[] buffer = Encoding.ASCII.GetBytes(sInputValue);
return ByteCRC(ref buffer);
} /// <summary>
/// 文件校验
/// </summary>
/// <param name="sInputFilename">文件名</param>
/// <returns></returns>
public static ulong FileCRC(string sInputFileName)
{
FileStream inFile = new System.IO.FileStream(sInputFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] bInput = new byte[inFile.Length];
inFile.Read(bInput, 0, bInput.Length);
inFile.Close(); return ByteCRC(ref bInput);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货