建立 crc32.cpp
代码如下:
---------------------------------------------------------------------------------
//kwanhong Young 2004-9-1
unsigned long CRCTable[256];
/* int main(int argc, char* argv[]) { Init(); unsigned long r=Update((unsigned char *)("ABCDEFG"), 7,0xFFFFFFFF); printf("result is:%d",r); return 0; } */
//void Init() extern "C" void __stdcall Init() { unsigned long crc, i, j; unsigned long poly = 0xEDB88320;
for(i=0; i<256; i++) { crc = i; for(j=8; j>0; j--) { if(crc & 1) crc = (crc >> 1) ^ poly; else crc >>= 1; } CRCTable[i] = crc; } }
extern "C" unsigned long __stdcall Update(unsigned char *buffer,long length, unsigned long crc) { //unsigned long crc = 0xFFFFFFFF; unsigned char *ptr = buffer;
while(length > 0) { crc = ((crc >> 8) & 0x00FFFFFF) ^ CRCTable[(crc ^ *ptr) & 0xFF]; ptr++; length--; } return crc; }
创建 crc32.def
代码如下:
---------------------------------------------------------------------------
EXPORTS Init Update
然后就可以编译成 DLL 了。
现在用VB新建一个 Class。代码如下:
-------------------------------------------------------------------------
Private Declare Sub CRC32Init Lib "kw_crc32.dll" Alias "Init" () Private Declare Function CRC32Update Lib "kw_crc32.dll" Alias "Update" (db As Any, ByVal length As Long, ByVal crcvalue As Long) As Long
Private m_lValue As Long
Private Sub Class_Initialize() ''''//make crc32 table CRC32Init m_lValue = &HFFFFFFFF End Sub
Public Sub Reset() m_lValue = &HFFFFFFFF End Sub
Public Sub Encode(bytData() As Byte, ByVal iLen As Long) m_lValue = CRC32Update(bytData(0), iLen, m_lValue) End Sub
Public Sub EncodePtr(ByVal ptbytData As Long, ByVal iLen As Long) ''''//use data pointer m_lValue = CRC32Update(ByVal ptbytData, iLen, m_lValue) End Sub
Public Function GetValue() As String GetValue = Right("00000000" & Hex$(m_lValue Xor &HFFFFFFFF), 8) End Function
Public Function EncodeString(SourceString As String) As String ''''// Function to digest a text string and output the result as a string ''''// of hexadecimal characters. Dim bytData() As Byte m_lValue = &HFFFFFFFF If Len(SourceString) = 0 Then ReDim bytData(0) EncodeString = "00000000" Else bytData = StrConv(SourceString, vbFromUnicode) m_lValue = CRC32Update(bytData(0), UBound(bytData) + 1, m_lValue) End If EncodeString = Right("00000000" & Hex$(m_lValue Xor &HFFFFFFFF), 8) End Function
Public Function SaveState() As String ''''//save the state SaveState = m_lValue End Function
Public Function LoadState(s As String) As Boolean ''''//load the state m_lValue = s LoadState = True End Function
现在就可以在VB里面实现高速的CRC32 运算。
[VB.NET程序]GSM短信模块库函数,可以用VB,VC,调用简单实用 [C语言系列]C#注册表的读,写,删除,查找 [网页制作]DIV CSS布局:自定义标签布局页面 [操作系统]svchost.exe进程详解之由浅入深 [电脑技术]使用?和*两个通配符迅速批量(搜索)查找文件 [聊天工具]快速又高效 用Email免费发送网络传真__天极Yesky [常用软件]网管工具 用OH协助服务器进行安全监控 [常用软件]留住精彩 用“数码故事”制作DVD光碟 [常用软件]小块头有大智慧 用Nero轻松设计个性化光盘 [常用软件]技高一筹 用Winamp播放影碟
|