|
-
Mar 25th, 2013, 06:15 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] cyclic redundency check software
I am using some external software that creates a crc.
For each line of text it creates a crc - i.e. the last field in the following example "71BACE4E"
EGPK,OBST,LIGHTHOUSE,553137.73N,0044402.31W,75.45,247.52,N,227527.27,629330.31,20.79,68.21,1000,04/01/12,71BACE4E
It is described as CRC-32Q,Hex
I would like to do the same using my software to create the same crc value.
I'm not sure if all crc software will create the same crv value - I would imagine that it will vary depending upon the algorithm/ polynomial used.
Is there some software I can get that I can link into my project so that it will create this crc value?
Sorry, I'm not sure if this is the correct place to ask this question.
Thanks
-
Mar 25th, 2013, 02:34 PM
#2
Re: cyclic redundency check software
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 27th, 2013, 07:17 AM
#3
Thread Starter
Hyperactive Member
Re: cyclic redundency check software
Thanks,
I used this code - it's in C++.
It works but gives a different CRC as the other software.
I think there can only be two differences - the input text string and the polynomial.
I can change the code so the polynomial is CRC-32Q - but the input text string must be different as it's giving a different crc.
http://www.codeproject.com/Articles/...-HashAlgorithm
-
Mar 28th, 2013, 05:50 AM
#4
Re: cyclic redundency check software
The text string you gave as an example in Post #1 is being treated as a record with 14 fields, each field delimited by a comma. For the purpose of calculating the CRC, the fields are being concatenated into one long string without the commas. i.e. the string you need to pass to your CRC algorithm is
EGPKOBSTLIGHTHOUSE553137.73N0044402.31W75.45247.52N227527.27629330.3120.7968.21100004/01/12
The algorithm you linked to in Post #3 looks very similar to the one linked to by the thread that .paul. linked to. It's a very popular algorithm and can also be found on this forum in the code bank.
Unfortunately, it's not the CRC-32Q algorithm.
If you are wanting to calculate CRC-32Qs just for short strings, then the following function may be of help. It is very naive and very un-optimised, but it seems to work with the strings that I have tested so far (yes, both of them ).
vb.net Code:
Private Function GetCRC32Q(data As String) As UInteger
Dim crc As UInteger = 0
Dim mustXOR As Boolean = False
Dim newbit As UInteger = 0
Dim polyQ As UInteger = &H814141ABUI
' convert the string to bytes. Use the appropriate Encoding
Dim bytes As New List(Of Byte)(Encoding.UTF8.GetBytes(data))
' must add 4 'empty' bytes to the end
bytes.AddRange(New Byte() {0, 0, 0, 0})
For indx As Integer = 0 To bytes.Count - 1
For bit = 7 To 0 Step -1
' need to XOR with poly if most significant bit of crc is set....
mustXOR = (crc And &H80000000UI) <> 0
' .... but first, must shift crc 1 bit to left, losing top bit
' while shifting in next bit from data byte as bottom bit of crc
If (bytes(indx) And (1 << bit)) <> 0 Then newbit = 1 Else newbit = 0
crc = (crc << 1) Or newbit
' now do the XOR, if needed
If mustXOR Then crc = crc Xor polyQ
Next bit
Next
Return crc
End Function
and you would use it as:
Code:
Dim originalString As String = "EGPK,OBST,LIGHTHOUSE,553137.73N,0044402.31W,75.45,247.52,N,227527.27,629330.31,20.79,68.21,1000,04/01/12"
Dim concatenatedFields As String = originalString.Replace(",", "")
Dim crc32Q As UInteger = GetCRC32Q(concatenatedFields)
MessageBox.Show(crc32Q.ToString("X"))
A few things to consider:
The text encoding (UTF8) may not be correct. I have no idea how your 3rd party App handles the encoding.
If the fields themselves contain commas (unlikely), you would need to use a different method for splitting and then recombining the fields without the delimiters.
If you have a lot of data to process, you may need the more efficient algorithm that uses a pre-calculated table. It's not that difficult to code, should you be interested.
Most of the info regarding CRC algorithms/parameters came from
http://www.ross.net/crc/download/crc_v3.txt and
http://reveng.sourceforge.net/crc-ca...rc.cat-bits.32
-
Mar 28th, 2013, 06:26 AM
#5
Thread Starter
Hyperactive Member
Re: cyclic redundency check software
Thanks, that was very helpful.
I had tried removing the commas but didn't realise that I had the wrong algotithm!
I will do some testing on the data I have to see if it gives the correct results.
-
Mar 28th, 2013, 07:12 AM
#6
Thread Starter
Hyperactive Member
Re: cyclic redundency check software
I wrote a short program to read the files line by line and check the crc.
The only problem I encountered is there the original text is:
"EGSS,OBST,PYLON,515033.39N,0000622.19E,185.09,607.26,N,545187.43,218096.70,139.34,457.15,1034,1 5/07/09"
the calculated CRC is "B84A1AC"
the correct CRC is "0B84A1AC".
I have written a few lines of code to correct this but maybe you could change your program more elegently to cope with this problem.
Thans again.
-
Mar 28th, 2013, 07:52 AM
#7
Re: cyclic redundency check software
The function currently returns the CRC as a UInteger so that you can convert it to a string for display in any format you desire.
If you are only interested in an 8 digit hexadecimal string with leading zeros, it probably makes sense to change it as:
vb.net Code:
Private Function GetCRC32Qhex(data As String) As String
Dim crc As UInteger = 0
Dim mustXOR As Boolean = False
Dim newbit As UInteger = 0
Dim polyQ As UInteger = &H814141ABUI
' convert the string to bytes. Use the appropriate Encoding
Dim bytes As New List(Of Byte)(Encoding.UTF8.GetBytes(data))
' must add 4 'empty' bytes to the end
bytes.AddRange(New Byte() {0, 0, 0, 0})
For indx As Integer = 0 To bytes.Count - 1
For bit = 7 To 0 Step -1
' need to XOR with poly if most significant bit of crc is set....
mustXOR = (crc And &H80000000UI) <> 0
' .... but first, must shift crc 1 bit to left, losing top bit
' while shifting in next bit from data byte as bottom bit of crc
If (bytes(indx) And (1 << bit)) <> 0 Then newbit = 1 Else newbit = 0
crc = (crc << 1) Or newbit
' now do the XOR, if needed
If mustXOR Then crc = crc Xor polyQ
Next bit
Next
Return crc.ToString("X8")
End Function
and call it as:
Code:
Dim originalString As String = "EGSS,OBST,PYLON,515033.39N,0000622.19E,185.09,607.26,N,545187.43,218096.70,139.34,457.15,1034,15/07/09"
Dim concatenatedFields As String = originalString.Replace(",", "")
Dim crc32Qhex As String = GetCRC32Qhex(concatenatedFields)
MessageBox.Show(crc32Qhex)
Return
(I removed the spurious space from the date field as it was giving the wrong checksum )
-
Mar 28th, 2013, 08:21 AM
#8
Thread Starter
Hyperactive Member
Re: cyclic redundency check software
Thanks, that is working correctly.
Thanks again for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|