Wrox Programmer Forums
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 27th, 2005, 04:50 PM
Authorized User
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Crc32 ;_;

Hi guys and gals,

I'm having a little trouble here. I'm in need of finding a way to get the crc32 of a string value in vb.net. Wondering if anyone has the function already built, or know of where I can get one?

I have come close to finding it, and I'll list the code below. The problem that I have with the code below is that its made to be used to crc a file... if anyone is able to translate this to be used on a string it would be MUUUUCHLY appreciated! (This snippet is from an archived forum, so contacting the author has not been helpful).

Public Class CRC32
    Private crc32Table() As Integer
    Private Const BUFFER_SIZE As Integer = 1024

    Public Function GetCrc(ByRef stream As System.IO.Stream) As Integer

        Dim crc32Result As Integer
        crc32Result = &HFFFFFFFF

        Dim buffer(BUFFER_SIZE) As Byte
        Dim readSize As Integer = BUFFER_SIZE

        Dim count As Integer = stream.Read(buffer, 0, readSize)
        Dim i As Integer
        Dim iLookup As Integer
        Dim tot As Integer = 0
        Do While (count > 0)
            For i = 0 To count - 1
                iLookup = (crc32Result And &HFF) Xor buffer(i)
                crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF
                crc32Result = crc32Result Xor crc32Table(iLookup)

            Next i
            count = stream.Read(buffer, 0, readSize)

        GetCrc = Not (crc32Result)
    End Function

    Public Sub New()
        Dim dwPolynomial As Integer = &HEDB88320
        Dim i As Integer, j As Integer

        ReDim crc32Table(256)
        Dim dwCrc As Integer

        For i = 0 To 255
            dwCrc = i
            For j = 8 To 1 Step -1
                If (dwCrc And 1) Then
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                    dwCrc = dwCrc Xor dwPolynomial
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                End If
            Next j
            crc32Table(i) = dwCrc
        Next i
    End Sub

End Class
Old April 27th, 2005, 04:58 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

What exactly is it that you are trying to do?
Inasmuch as characters are not 32 bits, is it that you just want to keep loading an integer with the ASCII for each character, storing this into a 32-bit number, throwing away the bits that would roll into the 33rd bit?

I suspect there is a much easier way to skin this cat...
Old April 28th, 2005, 09:55 AM
Authorized User
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts

Basically all im trying to do (the code above was just something I was hoping someone could use) is to get the crc32 of a string...

dim crc32val as integer
dim ToCrcString as string

ToCrcString = "wrox"
crc32val = Crc32(toCrcString)

Thats pretty much what I want to do, Im sorry if Im making things more complicated than it really is, im just not very familiar with crc32.

Old April 28th, 2005, 10:05 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

hi there..

just change the function definition.. pass a string to it... store it in a bit array inside buffer, take away all the code that refer to a file and done..

what are your question about the function?? do you have problems cleaning it???


Old April 28th, 2005, 03:47 PM
Authorized User
Join Date: Mar 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts

Ya that was my problem, I couldnt clean it.

Anyway, I did find out how, thanks for the help tho! :D

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.