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

Posted on 2006-04-20
Last Modified: 2010-04-23
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,

Question by:cssgyula

    Author Comment

    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
                    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!

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 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

    17 Experts available now in Live!

    Get 1:1 Help Now