Solved

CRC16 translation from C to VB6

Posted on 2006-11-29
7
1,766 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now