2013-03-21 68 views
2

是否有VB.NET计算字符串或字节数组的CRC32的函数或示例?计算字符串或字节数组的CRC32

+1

是[这](http://stackoverflow.com/questions/8128/how-do-i-calculate-crc32-of-a-string)你需要什么。 ? – 2013-03-21 17:12:46

+0

其实我看过之前,但他们没有作品,其中大部分链接是用于计算文件的CRC32 – Shahriyar 2013-03-21 17:20:00

+1

“非他们的作品”真的,他们有什么不对? – Magnus 2013-03-21 17:29:08

回答

9

使用此:

Private Sub Main() 
    Crc32.ComputeChecksum(Encoding.UTF8.GetBytes("Some string")).Dump() 
End Sub 

Public Class Crc32 
    Shared table As UInteger() 

    Shared Sub New() 
     Dim poly As UInteger = &Hedb88320UI 
     table = New UInteger(255) {} 
     Dim temp As UInteger = 0 
     For i As UInteger = 0 To table.Length - 1 
      temp = i 
      For j As Integer = 8 To 1 Step -1 
       If (temp And 1) = 1 Then 
        temp = CUInt((temp >> 1) Xor poly) 
       Else 
        temp >>= 1 
       End If 
      Next 
      table(i) = temp 
     Next 
    End Sub 

    Public Shared Function ComputeChecksum(bytes As Byte()) As UInteger 
     Dim crc As UInteger = &HffffffffUI 
     For i As Integer = 0 To bytes.Length - 1 
      Dim index As Byte = CByte(((crc) And &Hff) Xor bytes(i)) 
      crc = CUInt((crc >> 8) Xor table(index)) 
     Next 
     Return Not crc 
    End Function 
End Class 
+0

工作很好,谢谢 – Shahriyar 2013-03-21 18:17:33

+0

小而甜 – Khan 2015-08-01 11:22:03