PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2012 Code return CRC32B instead of CRC32-VBForums
Results 1 to 13 of 13

Thread: Code return CRC32B instead of CRC32

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Code return CRC32B instead of CRC32

    Hi,

    I am trying to convert a string into CRC32.
    I used the code found online below and it returns CRC32B (as cross checked in one of the free online CRC32 generator e.g. http://www.md5calc.com/) instead of CRC32.

    Code:
    Imports System.Text
    
    Public Class CRC32
        Private Shared CrcTable As UInt32() = Nothing
    
        Shared Sub New()
            Dim c As UInt32
            Dim One As UInt32 = 1
            Dim Filter As UInt32 = 3988292384
            Dim i As Integer
            Dim j As Integer
    
            CrcTable = New UInt32(255) {}
            For i = 0 To 255
                c = Convert.ToUInt32(i)
                For j = 0 To 7
                    If (c And One) <> 0 Then
                        c = Filter Xor (c >> 1)
                    Else
                        c = c >> 1
                    End If
                Next
                CrcTable(i) = c
            Next
        End Sub
    
        Public Shared Function Calculate(ByVal s As String) As String
            Dim InitVal As UInt32 = 4294967295
            Dim CRC As UInt32 = InitVal
            Dim i As Integer
    
            Dim buffer As Byte() = Encoding.UTF8.GetBytes(s)
            For i = 0 To buffer.Length - 1
                CRC = CrcTable((CRC Xor buffer(i)) And 255) Xor (CRC >> 8)
            Next
    
            Return Convert.ToUInt32(CRC Xor InitVal).ToString("X8")
        End Function
    End Class
    I'm not sure what went wrong and I couldn't find any more example. As most example online shows File conversion instead of String conversion.

    I also found another site that mentioned CRC32b is equal to CRC32 with the 4 bytes reversed.
    http://stackoverflow.com/questions/2...crc32b-in-java

    I tried to various ways to convert into VB.NET code but to no avail.
    Hope someone can enlighten me.

    Thanks.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,284

    Re: Code return CRC32B instead of CRC32

    Years ago (VS 2005 era) I had a need to compute the CRC32 of files on a hard drive and this is what I used at the time, you'd have to take out the reading of file bytes and simply pass in a string but the computation should still be the same.
    Attached Files Attached Files

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by JuggaloBrotha View Post
    Years ago (VS 2005 era) I had a need to compute the CRC32 of files on a hard drive and this is what I used at the time, you'd have to take out the reading of file bytes and simply pass in a string but the computation should still be the same.
    Thanks for the code.
    I found a few sample online that is also for File Conversion but I 'm not really sure how to convert from FileStream to String.

    I tried convert your code in this GetCrc32 function but there is no result (Application Not Responding).
    I have commented the original code.

    Not sure what went wrong.
    Hope you can enlighten me.

    Thanks.

    Code:
        ' <System.Diagnostics.DebuggerStepThrough()> Friend Function GetCrc32(ByRef stream As System.IO.FileStream) As Integer
        <System.Diagnostics.DebuggerStepThrough()> Friend Function GetCrc32(ByRef stream As String) As Integer
    
            Dim crc32Result As Integer = &HFFFFFFFF
    
            Dim buffer(BUFFER_SIZE) As Byte
            Dim readSize As Integer = BUFFER_SIZE
            ' Dim count As Integer = stream.Read(buffer, 0, readSize)
            Dim count As Integer = stream.Length
            Dim i As Integer
            Dim iLookup As Integer
    
            Do While (count > 0)
                For i = 0 To count - 1
                    iLookup = (crc32Result And &HFF) Xor buffer(i)
                    crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
                    crc32Result = crc32Result Xor crc32Table(iLookup)
                Next i
                ' count = stream.Read(buffer, 0, readSize)
                count = stream.Length
            Loop
            Return Not (crc32Result)
        End Function

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Code return CRC32B instead of CRC32

    I have some code that uses tables for calculating CRC's if you are interested.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,284

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by abcat View Post
    Thanks for the code.
    I found a few sample online that is also for File Conversion but I 'm not really sure how to convert from FileStream to String.

    I tried convert your code in this GetCrc32 function but there is no result (Application Not Responding).
    I have commented the original code.

    Not sure what went wrong.
    Hope you can enlighten me.

    Thanks.

    Code:
        ' <System.Diagnostics.DebuggerStepThrough()> Friend Function GetCrc32(ByRef stream As System.IO.FileStream) As Integer
        <System.Diagnostics.DebuggerStepThrough()> Friend Function GetCrc32(ByRef stream As String) As Integer
    
            Dim crc32Result As Integer = &HFFFFFFFF
    
            Dim buffer(BUFFER_SIZE) As Byte
            Dim readSize As Integer = BUFFER_SIZE
            ' Dim count As Integer = stream.Read(buffer, 0, readSize)
            Dim count As Integer = stream.Length
            Dim i As Integer
            Dim iLookup As Integer
    
            Do While (count > 0)
                For i = 0 To count - 1
                    iLookup = (crc32Result And &HFF) Xor buffer(i)
                    crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
                    crc32Result = crc32Result Xor crc32Table(iLookup)
                Next i
                ' count = stream.Read(buffer, 0, readSize)
                count = stream.Length
            Loop
            Return Not (crc32Result)
        End Function
    Short and simple, to calculate the CRC of anything you need to do it at the byte level, so the function to calculate the bytes is:
    vb.net Code:
    1. Private Function GetCrc32(bytes As Byte()) As Integer
    2.     Dim table(255) As Integer
    3.     Dim poly As Integer = &HEDB88320I
    4.     Dim temp As Integer = 0
    5.     For i As Integer = 0 To table.Length - 1
    6.         temp = i
    7.         For j As Integer = 8 To 1 Step -1
    8.             If (temp And 1) = 1 Then
    9.                 temp = (temp >> 1) Xor poly
    10.             Else
    11.                 temp >>= 1
    12.             End If
    13.         Next
    14.         table(i) = temp
    15.     Next i
    16.     Dim crc As Integer = &HFFFFFFFFI
    17.     For i As Integer = 0 To bytes.Length - 1
    18.         Dim index As Byte = CByte(((crc) And &HFF) Xor bytes(i))
    19.         crc = (crc >> 8) Xor table(index)
    20.     Next
    21.     Return Not crc
    22. End Function
    And to call it, just pass in the bytes of the text you want calculated:
    vb.net Code:
    1. Imports System.Text
    2.  
    3. TextBox2.Text = GetCrc32(Encoding.UTF8.GetBytes(TextBox1.Text)).ToString()

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by JuggaloBrotha View Post
    Short and simple, to calculate the CRC of anything you need to do it at the byte level, so the function to calculate the bytes is:
    vb.net Code:
    1. Private Function GetCrc32(bytes As Byte()) As Integer
    2.     Dim table(255) As Integer
    3.     Dim poly As Integer = &HEDB88320I
    4.     Dim temp As Integer = 0
    5.     For i As Integer = 0 To table.Length - 1
    6.         temp = i
    7.         For j As Integer = 8 To 1 Step -1
    8.             If (temp And 1) = 1 Then
    9.                 temp = (temp >> 1) Xor poly
    10.             Else
    11.                 temp >>= 1
    12.             End If
    13.         Next
    14.         table(i) = temp
    15.     Next i
    16.     Dim crc As Integer = &HFFFFFFFFI
    17.     For i As Integer = 0 To bytes.Length - 1
    18.         Dim index As Byte = CByte(((crc) And &HFF) Xor bytes(i))
    19.         crc = (crc >> 8) Xor table(index)
    20.     Next
    21.     Return Not crc
    22. End Function
    And to call it, just pass in the bytes of the text you want calculated:
    vb.net Code:
    1. Imports System.Text
    2.  
    3. TextBox2.Text = GetCrc32(Encoding.UTF8.GetBytes(TextBox1.Text)).ToString()
    I tried your code and convert string "abc" to CRC32 and it returns "-347100478"
    The expected output should be "73bb8c64" according to http://www.md5calc.com/

    Please advise.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by dbasnett View Post
    I have some code that uses tables for calculating CRC's if you are interested.
    Hi dbasnett,

    I found various code online which is convert from File to CRC32. I'm not sure whether your code can calculate text string and convert it into CRC32?

    Thanks.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,798

    Re: Code return CRC32B instead of CRC32

    you don't convert it into CRC32... you calculate the CRC32 value based on the input. The input is just an array of bytes... doesn't matter if it's "binary" data (which, technically, everything is)... or string... I think string objects have a .ToBinaryArray method that will return an array of bytes representing the string...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Code return CRC32B instead of CRC32

    This site gives full control to all aspects of CRC calculation

    http://www.sunshine2k.de/coding/java...rc/crc_js.html

    FWIW - -347100478 in hex is EB4FAAC2
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by dbasnett View Post
    This site gives full control to all aspects of CRC calculation

    http://www.sunshine2k.de/coding/java...rc/crc_js.html

    FWIW - -347100478 in hex is EB4FAAC2
    The sunshine2k site convert input string "abc" to "0x352441C2" which is CRC32B. My first thread codes already can work with CRC32B but I'm not sure how to convert to CRC32.

    The md5calc site convert input string "abc" to "73bb8c64" which is CRC32 which is what I want.

    You mentioned 347100478 in hex is EB4FAAC2. Seems like after this conversion, the output is still not CRC32 or CRC32B.

    I'm really stuck in converting my initial code into CRC32 and also can't find any VB.NET to calculate string to CRC32 output.

    Please enlighten me further.
    Thanks.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by abcat View Post
    The sunshine2k site convert input string "abc" to "0x352441C2" which is CRC32B. My first thread codes already can work with CRC32B but I'm not sure how to convert to CRC32.

    The md5calc site convert input string "abc" to "73bb8c64" which is CRC32 which is what I want.

    You mentioned 347100478 in hex is EB4FAAC2. Seems like after this conversion, the output is still not CRC32 or CRC32B.

    I'm really stuck in converting my initial code into CRC32 and also can't find any VB.NET to calculate string to CRC32 output.

    Please enlighten me further.
    Thanks.
    For any implementation there is (from the sunshine page):

    Width (in bits): Defines the width of the result CRC value (n bits). Simultaneously, also the width of the generator polynomial is defined (n+1 bits). Most common used widths are 8, 16 and 32 bit. But thereotically all widths beginning from 1 are possible. In practice, even quite big (80 bit) or uneven (5 bit or 31 bit) widths are used.

    Polynomial: Used generator polynomial value. Note that different representations exist.

    Initial Value: The value used to initialize the CRC value / register. In the examples above, always zero is used, but it could be any value.

    Input reflected: If this value is TRUE, each input byte is reflected before being used in the calculation. Reflected means that the bits of the input byte are used in reverse order. So this also means that bit 0 is treated as the most significant bit and bit 7 as least significant.

    Result reflected: If this value is TRUE, the final CRC value is reflected before being returned. The reflection is done over the whole CRC value, so e.g. a CRC-32 value is reflected over all 32 bits.

    Final XOR value: The Final XOR value is xored to the final CRC value before being returned. This is done after the 'Result reflected' step. Obviously a Final XOR value of 0 has no impact.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    355

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by dbasnett View Post
    For any implementation there is (from the sunshine page):

    Width (in bits): Defines the width of the result CRC value (n bits). Simultaneously, also the width of the generator polynomial is defined (n+1 bits). Most common used widths are 8, 16 and 32 bit. But thereotically all widths beginning from 1 are possible. In practice, even quite big (80 bit) or uneven (5 bit or 31 bit) widths are used.

    Polynomial: Used generator polynomial value. Note that different representations exist.

    Initial Value: The value used to initialize the CRC value / register. In the examples above, always zero is used, but it could be any value.

    Input reflected: If this value is TRUE, each input byte is reflected before being used in the calculation. Reflected means that the bits of the input byte are used in reverse order. So this also means that bit 0 is treated as the most significant bit and bit 7 as least significant.

    Result reflected: If this value is TRUE, the final CRC value is reflected before being returned. The reflection is done over the whole CRC value, so e.g. a CRC-32 value is reflected over all 32 bits.

    Final XOR value: The Final XOR value is xored to the final CRC value before being returned. This is done after the 'Result reflected' step. Obviously a Final XOR value of 0 has no impact.
    Thanks for your response but this is too technical for me and I still do not understand how do I utilize or convert to the format that I want. Seems like there is no straightforward function to achieve that via VB.NET.

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Code return CRC32B instead of CRC32

    Quote Originally Posted by abcat View Post
    Thanks for your response but this is too technical for me and I still do not understand how do I utilize or convert to the format that I want. Seems like there is no straightforward function to achieve that via VB.NET.
    What I am trying to say is that a specific implementation of CRC is dependent upon those variables. So my base CRC32 implementation is:

    Width (in bits): Defines the width of the result CRC value (n bits). Simultaneously, also the width of the generator polynomial is defined (n+1 bits). Most common used widths are 8, 16 and 32 bit. But thereotically all widths beginning from 1 are possible. In practice, even quite big (80 bit) or uneven (5 bit or 31 bit) widths are used.

    Polynomial: 0x04C11DB7

    Initial Value: 0

    Input reflected: False

    Result reflected: False

    Final XOR value: 0

    When I go to the sunshine page and use those values, sunshine and I agree about a given CRC. If your CRC is not matching then it is because of one of those variables. The algorithm is fairly straight forward and doesn't change much based on the settings.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width