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.
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.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
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
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:
Private Function GetCrc32(bytes As Byte()) As Integer
Dim table(255) As Integer
Dim poly As Integer = &HEDB88320I
Dim temp As Integer = 0
For i As Integer = 0 To table.Length - 1
temp = i
For j As Integer = 8 To 1 Step -1
If (temp And 1) = 1 Then
temp = (temp >> 1) Xor poly
Else
temp >>= 1
End If
Next
table(i) = temp
Next i
Dim crc As Integer = &HFFFFFFFFI
For i As Integer = 0 To bytes.Length - 1
Dim index As Byte = CByte(((crc) And &HFF) Xor bytes(i))
crc = (crc >> 8) Xor table(index)
Next
Return Not crc
End Function
And to call it, just pass in the bytes of the text you want calculated:
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/
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...
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.
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.
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.
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.