Solved

CRC16 translation from C to VB6

Posted on 2006-11-29
7
1,771 Views
Last Modified: 2008-02-01
I have been given a CRC algorithm written in C and i need it in VB.  I have tried several times but am unable to reproduce the results.  Here is the C code:

CRC_POLYNOM = 0x8408
CRC_PRESET = 0xFFFF

unsigned internal CRC = CRC_PRESET

for(i=0; i < cnt; i++)
{
crc ^= DATA[i]
for(j=0; j<8;j++)
{
if(crc & 0x0001)
crc=(crc>>1)^CRC_POLYNOM;
else
crc=(crc>>1);
}
}

Thanks!!!
0
Comment
Question by:mgouzie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 

Expert Comment

by:Pfurz
ID: 18043545
You can use the vb function below to calculate a CRC16 checksum from a string.

Public Function CALC_CRC16(ByRef strMessage As String) As Long

Dim bTemp As Byte
Dim crcrhig As Byte
Dim lStrPos As Long
Dim Character As Byte
Dim iCarry As Integer
Dim lResult As Long
Dim lResultHigh As Long
Dim lPolonium As Long
Dim lCounter As Long

lPolonium = 32773
lResult = 0

For lStrPos = 1 To Len(strMessage)
   
   Character = Asc(Mid(strMessage, lStrPos, 1))
   bTemp = (Character And &H7F) * 2
   lResultHigh = lResult / 2 ^ 8
   lResultHigh = lResultHigh Xor bTemp
   lResult = (lResult And &HFF) Or (lResultHigh * 2 ^ 8)
   
   For lCounter = 0 To 7
       iCarry = (lResult And &H8000) / 2 ^ 15
       lResult = (lResult And &H7FFF) * 2
       If iCarry <> 0 Then
           lResult = lResult Xor lPolonium
       End If
   Next lCounter

Next lStrPos

CALC_CRC16 = lResult

End Function
0
 

Author Comment

by:mgouzie
ID: 18045876
Well I dont exactly need to compute it on a string... I am new to CRC's at this level, so please pardon my ignorance. I have to go both ways with the CRC, both check a given CRC as well as compute a new pair of CRC values.  The data I am checking is in hex format.  
For an example...
I have 0x05FF65 that needs to have CRC's generated for it.  I know for a fact that the proper CRC's for this are 0xE5CB.
I have to match the algorithm I am given exactly because I am dealing with a piece of hardware with an embedded CRC check/generator on it and no way of changing it. Thanks for your help.
0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 500 total points
ID: 18048587
I've converted that C function you gave...here is a direct conversion that takes a byte array (the same input as the one above).


Private Const CRC_POLYNOM As Long = &H8408
Private Const CRC_PRESET As Long = &HFFFF
Private Function crc(data() As Byte) As Long
    Dim lCRC As Long, lCount As Long, lCount2 As Long
   
    lCRC = CRC_PRESET                          'unsigned internal CRC = CRC_PRESET
    For lCount = 0 To UBound(data)             'for(i=0; i < cnt; i++)
        lCRC = lCRC Xor data(lCount)           'crc ^= DATA[i]
        For lCount2 = 0 To 8                   'for(j=0; j<8;j++)
            If (lCRC And 1) Then               'if(crc & 0x0001)
             lCRC = (lCRC / 2) Xor CRC_POLYNOM 'crc=(crc>>1)^CRC_POLYNOM;
            Else
             lCRC = (lCRC / 2)                 'crc=(crc>>1)
            End If
        Next lCount2
    Next lCount
    crc = lCRC
End Function


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Pfurz
ID: 18050303
You can't use hex values like "0x05FF65" with vb. You must convert them into a long value:

Public Function HexToLong(ByVal strHexValue As String) As Long
   HexToLong = Val("&H" & strHexValue & "&")
End Function

HexToLong("05FF65") = 393061
HexToLong("E5CB") = 58827

Without the exact knowlegde about the embedded CRC routine of your hardware its not possible to create a proper function that do the same under vb. Do you have more specific informations about the embedded CRC routine of your hardware?
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18050639
Pfurz you can use hex values; however, you must prefix them with &H insted of 0x.

Dim x as long
x = &H05FF65
x = &HE5CB


0
 

Author Comment

by:mgouzie
ID: 18053430
Thanks for the conversion, I had to add a couple things to make the actual CRC check work properly, but that translation was the jump start I needed. Cheers!
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18055825
Glad I could help :)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question