Solved

CRC16 translation from C to VB6

Posted on 2006-11-29
7
1,763 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now