Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Checksum function

I am trying to calculate the checksum (which is to obtain the sum of all bytes then obtain the 1's complement) - then store this as 1 byte to a file.  I am running on a Linux platform.

I received some feedback from the experts and was given this calculation -
unsigned char Data[12];
int i;

unsigned int CRC ()
{
  unsigned int sum;

  sum = 0;
  for (i=0; i < 12; ++i)
    sum += Data[i];

  sum ^= sum;
  return (sum);
}

However, i had some issues with it - it was populating some bizarre # -



When I use this approach  - I believe it works.  

#define       POLYNOMIAL      0x04C11DB7      
#define       WIDTH    (8 * sizeof(unsigned char))                                                                        
#define       TOPBIT   (1 << (WIDTH - 1))
      
  for (int byte = 0; byte < dataSize; ++byte)  {
       remainder ^= (data[byte] << (WIDTH - 8));
      for (unsigned char bit = 8; bit > 0; --bit) {    
                if (remainder & TOPBIT)
                              remainder = (remainder << 1) ^ POLYNOMIAL;
                         else
                         remainder = (remainder << 1);
                  }
                }
      }      
      return remainder;

I would like to  understand what the differences are between the 2 implementations.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jewee
jewee

ASKER

I was going to post this question int he previous one but I already closed it out.  

I was just curious - What are the differences in calculation???  do they both generate pretty much the same thing - just different implementation?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi jewee,

> I was going to post this question int he previous one but I already
> closed it out.  

This is a legitimate follow up to the original post.  Don't be bashful about asking for clarification after closing a question.  Most of use that respond in a thread will revisit it after another post.


Kent
Avatar of jewee

ASKER

Thank you Kent.  I wasn't aware of the fact that I could do that after it was closed.