How to convert this C or C++ function to VB.NET function

Dear everyone,

I am programming a Serial Port Communicator Interface, my only problem is, that I cannot convert this C (or C++, don't really know which type of) function to work correctly with Visual Basic .NET in Visual Studio 2005.

This function generates a CRC checksum value for a specified command string. The command string start with STX, followed by the command, which can be max. 255 characters long and ends with ETX. In order to get my command working I need this function to calculate the CRC for my string.

The check sum is an 8 bit CRC from the beginning of the text (STX) to the end of the text (ETX)
included all characters (STX/ETX as well). For the message can’t contain control characters, we
send/wait the CRC converted into two characters format in hex.

The function is as follows:

byte crc8(byte *str, int len)
{
int i, f;
byte data;
byte crc;
crc = 0;
while (len--) {
                    data = *str++;
                    for (i = 0; i < 8; ++i) {
                                                     f = 1 & (data ^ crc);
                                                     crc >>= 1;
                                                     data >>= 1;
                                                     if (f) {
                                                              crc ^= 0x8c;
                                                             }
                                                     }
                   }
return crc;
}

What I need is to convert this function to Visual Basic .NET to calculate the correct CRC value for my command string.

This is a most urgent problem, because I cannot continue my project, if I cannot solve this issue.

Thank you for your help,

Cssgyula
cssgyulaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cssgyulaAuthor Commented:
Hi there,

After a non-stop 8 hours brainstroming and C++ book-reading I have finally made it possible to convert the code to VB.NET. I have already tested it on the device and is working correctly.

The translation is as follows:

Function Check_CRC(ByVal CommandString As String) As String
        Dim index, i, f As Integer
        Dim crc, data As Byte
        Dim Length As Integer = CommandString.Length
        Dim HexaConstant As Byte = Convert.ToByte("8C", 16)
        crc = 0
        index = 0

        While System.Math.Max(System.Threading.Interlocked.Decrement(Length), Length + 1)
            data = Asc(CommandString(index))
            index += 1
            i = 0
            Do
                f = 1 And (data Xor crc)
                crc >>= 1
                data >>= 1
                If (f) Then
                    crc = crc Xor HexaConstant
                End If
                i += 1
            Loop Until i = 8
        End While
        Dim ResultStr As String = CommandString & Hex(crc)
        Return ResultStr
    End Function

Anyway, thank you for reading this question!
0
EE_AutoDeleterCommented:
cssgyula,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.