Results 1 to 8 of 8

Thread: [RESOLVED] cyclic redundency check software

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Resolved [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: cyclic redundency check software


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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:
    1. Private Function GetCRC32Q(data As String) As UInteger
    2.     Dim crc As UInteger = 0
    3.     Dim mustXOR As Boolean = False
    4.     Dim newbit As UInteger = 0
    5.  
    6.     Dim polyQ As UInteger = &H814141ABUI
    7.  
    8.  
    9.     '   convert the string to bytes. Use the appropriate Encoding
    10.     Dim bytes As New List(Of Byte)(Encoding.UTF8.GetBytes(data))
    11.     '   must add 4 'empty' bytes to the end
    12.     bytes.AddRange(New Byte() {0, 0, 0, 0})
    13.  
    14.  
    15.     For indx As Integer = 0 To bytes.Count - 1
    16.         For bit = 7 To 0 Step -1
    17.             '   need to XOR with poly if most significant bit of crc is set....
    18.             mustXOR = (crc And &H80000000UI) <> 0
    19.  
    20.             '   .... but first, must shift crc 1 bit to left, losing top bit
    21.             '   while shifting in next bit from data byte as bottom bit of crc
    22.             If (bytes(indx) And (1 << bit)) <> 0 Then newbit = 1 Else newbit = 0
    23.             crc = (crc << 1) Or newbit
    24.  
    25.             '   now do the XOR, if needed
    26.             If mustXOR Then crc = crc Xor polyQ
    27.         Next bit
    28.     Next
    29.  
    30.     Return crc
    31. 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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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:
    1. Private Function GetCRC32Qhex(data As String) As String
    2.     Dim crc As UInteger = 0
    3.     Dim mustXOR As Boolean = False
    4.     Dim newbit As UInteger = 0
    5.  
    6.     Dim polyQ As UInteger = &H814141ABUI
    7.  
    8.  
    9.     '   convert the string to bytes. Use the appropriate Encoding
    10.     Dim bytes As New List(Of Byte)(Encoding.UTF8.GetBytes(data))
    11.     '   must add 4 'empty' bytes to the end
    12.     bytes.AddRange(New Byte() {0, 0, 0, 0})
    13.  
    14.  
    15.     For indx As Integer = 0 To bytes.Count - 1
    16.         For bit = 7 To 0 Step -1
    17.             '   need to XOR with poly if most significant bit of crc is set....
    18.             mustXOR = (crc And &H80000000UI) <> 0
    19.  
    20.             '   .... but first, must shift crc 1 bit to left, losing top bit
    21.             '   while shifting in next bit from data byte as bottom bit of crc
    22.             If (bytes(indx) And (1 << bit)) <> 0 Then newbit = 1 Else newbit = 0
    23.             crc = (crc << 1) Or newbit
    24.  
    25.             '   now do the XOR, if needed
    26.             If mustXOR Then crc = crc Xor polyQ
    27.         Next bit
    28.     Next
    29.  
    30.     Return crc.ToString("X8")
    31. 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 )

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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
  •  



Click Here to Expand Forum to Full Width