?
Solved

CRC16 translation from C to VB6

Posted on 2006-11-29
7
Medium Priority
?
1,785 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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