Do not use on any
shared computer
July 25, 2008 03:44pm pdt
null
[x]
Attachment Details

I need a checksum calculation written in C converted to VB

I have been given some C code which calculates a checksum value for a Hex string. It looks like it could be a CRC-8 checksum, but I'm not positive. I need to create a VB version of this but I'm afraid I don't know C very well and the second function is confusing me. Can anyone help?

unsigned char crc_buff(unsigned char *p, unsigned char I)
{
      unsigned char x;
      unsigned char crc;
            crc = 0xff;
            for ( x = 0; x < I; x++)
                  crc = crc8( crc, *p++);
            return crc;
}
// ---
unsigned char crc8(unsigned char crc; unsigned char data)
{
unsigned char i;
      crc = crc ^ data;
      for (i=0; i<8; i++){
            if (crc & 0x01)
                  crc = (crc >> 1) ^ 0x8C
            else
                  crc >>= 1;
      }      
      return crc;
}

Any help would be appreciated by this newbie.

Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: CONTRACTOR4SITE
Solution Provided By: PaulCaswell
Participating Experts: 3
Solution Grade: B
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Assisted Solution by roshkins

Rank: Master

Assisted Solution by roshkins:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by CONTRACTOR4SITE
Author Comment by CONTRACTOR4SITE:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by bandyt1712
Assisted Solution by bandyt1712:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by CONTRACTOR4SITE
Author Comment by CONTRACTOR4SITE:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by bandyt1712
Assisted Solution by bandyt1712:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by CONTRACTOR4SITE
Author Comment by CONTRACTOR4SITE:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by PaulCaswell

Rank: Guru

Accepted Solution by PaulCaswell:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by CONTRACTOR4SITE
Author Comment by CONTRACTOR4SITE:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by CONTRACTOR4SITE
Author Comment by CONTRACTOR4SITE:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
null
Comment by PaulCaswell
Thanks for the points.

I don't think you need:

                    CRC = CRC - 1

as it is odd at this point and you are just about to divide by 2 anyway.

Paul
 
null
Comment by CONTRACTOR4SITE
Hi Paul

Intersting point. I have tested it extensively and it works so I am reluctant to change it.
But I will bear your comment in mind should there be any further problems.
Thanks for the ongoing interest

Mike
 
null
Comment by PaulCaswell
Hi Mike,

If yours works reliably then this should too:

Public Function checksum8(HexString As String) As String

        Dim buff As Byte
        Dim CRC As Byte
        Dim x As Integer
        Dim y As Integer

        CRC = 255
       
        For x = 1 To Len(HexString) Step 2
       
            buff = hex2dec(Mid$(HexString, x, 2))
           
            CRC = CRC Xor buff
            For y = 1 To 8
                If CRC And 1 Then
                    CRC = CRC / 2       '     Shift Right 1 bit
                    CRC = CRC Xor 140   '     0x8C
                Else
                    CRC = CRC / 2       '     Shift Right 1 bit
                End If
            Next y
        Next x
       
        checksum8 = CRC

End Function

Note that you also don't need buff as an array as you only process it one byte at a time. Sorry to bang on but I just don't feel comfortable with code that is just 'space'.

Paul
 
null
Comment by CONTRACTOR4SITE
Now, that is an improvement. Can't argue with that. I will incorporate your version - interestingly I was concerned about the array, I didn't get it so I'm glad you've explained that it was redundant.
Mind you, if I have any problems you're now my Level 1 support! Hah!
 
null
Comment by CONTRACTOR4SITE
Paul
Thought you'd want to know. Buff doesn't need to be an array - as you rightly pointed out.
But removing the CRC = CRC -1 stopped it from working!
Re-instating this line restored functionality.
Interesting?!
Mike
 
null
Comment by PaulCaswell
>>But removing the CRC = CRC -1 stopped it from working!
Extraordinary!! I wonder why.

If CRC is odd then subtracting one and then dividing by two should have exactly the same effect as just dividing by 2 with integer types.

Perhaps VB recognises the bit loss and turns it into a float. Yukk!!! Glad I don't work in VB.

Paul
.
 
null
Comment by PaulCaswell
Cracked it!!

See Wikipedia on VB:

http://en.wikipedia.org/wiki/Visual_basic#Language_features

quote: ... Integers are automatically promoted to reals in expressions involving the normal division operator (/) so that division of an odd integer by an even integer produces the intuitively correct result. There is a specific integer divide operator (\) which does truncate.

So this should work using the '\' integer divide operator:

Public Function checksum8(HexString As String) As String

        Dim buff As Byte
        Dim CRC As Byte
        Dim x As Integer
        Dim y As Integer

        CRC = 255
       
        For x = 1 To Len(HexString) Step 2
       
            buff = hex2dec(Mid$(HexString, x, 2))
           
            CRC = CRC Xor buff
            For y = 1 To 8
                If CRC And 1 Then
                    CRC = CRC \ 2       '     Shift Right 1 bit
                    CRC = CRC Xor 140   '     0x8C
                Else
                    CRC = CRC \ 2       '     Shift Right 1 bit
                End If
            Next y
        Next x
       
        checksum8 = CRC

End Function



Paul
 
null
Comment by CONTRACTOR4SITE
I see you have the bit between your teeth.
Well, I'm off home (it's 10 PM here) but I promise to try this version when I'm back in the office on Thursday. You'll be the first to know! Thanks for your continuing interest.
M
 
null
Comment by PaulCaswell
10:00 pm here too. ;-) Haven't you got a home to go to. !!! :)
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628