Establish CRC32.cpp
code show as below:
-------------------------------------------------- -------------------------------
// kwanhong young 2004-9-1
Unsigned long crctable [256];
/ * int main (int Argc, char * argv []) {init (); unsigned long r = update ("Abcdefg"), 7,0xffffff); 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 = 0xfffffffff; unsigned char * ptr = buffer;
While (Length> 0) {CRC = ((CRC >> 8) & 0x00ffffff) ^ CRCTABLE [(CRC ^ * PTR) & 0xFF]; PTR ; Length--;} Return CRC;
Create CRC32.DEF
code show as below:
-------------------------------------------------- -------------------------
Exports Init Update
Then you can compile it into a DLL.
Now use VB to create a Class. code show as below:
-------------------------------------------------- -----------------------
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 = & HFFFFFFFFFEND SUB
Public Sub Reset () M_LValue = & HFFFFFFFFFEND SUB
Public Sub Encode (bytData () As Byte, ByVal iLen As Long) m_lValue = CRC32Update (bytData (0), iLen, m_lValue) End SubPublic 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 & hffffff), 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 & HFFFFFFFFFFFFfff, 8) End Function
Public Function SaveState () AS STRING '// Save The State SaveState = M_LValueEnd Function
PUBLIC FUNCTION LOADSTATE (S as String) as boolean '// load the state m_lvalue = s loadState = trueEnd Function
It is now possible to achieve high-speed CRC32 operations in VB.