本文介绍了计算字符串或字节数组的CRC32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有VB.NET的函数或例子来计算字符串或字节数组的CRC32? 使用这个:

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

公共类Crc32
共享表作为UInteger()

Shared Sub New()
Dim poly因为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)
其他
temp>> = 1
结束如果
下一个
表(i)=临时
下一个
结束Sub

公共共享函数ComputeCh ecksum(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)和& Hff)Xor字节(i))
crc = CUInt((crc>> 8)XOR表(索引))
下一个
返回不是crc
结束函数
结束类


Is there any function or example for VB.NET to calculate CRC32 of an string or Byte Array?

解决方案

Use this:

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

这篇关于计算字符串或字节数组的CRC32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 01:34