?
Solved

CRC16 translation from C to VB6

Posted on 2006-11-29
7
Medium Priority
?
1,778 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 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
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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