Solved

Checksum function

Posted on 2006-07-10
5
576 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:jewee
  • 2
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 300 total points
ID: 17076861
Hi jewee,

They are quite different algorithms.  The first solution (which I provided) asked the question that you answered.  i.e. sum the bytes and take the 1's complement.

The equation that you've provided solves the checksum issue in quite a bit different fashion than what you'd originally asked.


Good Luck!
Kent
0
 

Author Comment

by:jewee
ID: 17076922
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?
0
 
LVL 8

Assisted Solution

by:manish_regmi
manish_regmi earned 200 total points
ID: 17078431
As KDO said they are very different algorithms. The latter one is called CRC which is stronger then the first one.

please read the links
http://en.wikipedia.org/wiki/Checksum
http://en.wikipedia.org/wiki/Cyclic_redundancy_check

regards
Manish Regmi
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17078504
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
0
 

Author Comment

by:jewee
ID: 17078678
Thank you Kent.  I wasn't aware of the fact that I could do that after it was closed.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get time_t value that is a week old 6 169
Finding a good hash function 4 127
c++ getting the first 10 characters of a char* string 11 98
Super Scope, DHCP 5 91
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

809 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