Skip navigation.
 
mlRe: looking for a crc code
FROM : Steven Noyes
DATE : Sat Jun 14 18:55:49 2008

hi Angelo.

This is a trickier question than it sounds.  The first thing is you 
really have to understand is what you are after:

1) do you have an 8 bit, 16 bit or 32 bit CRC?

2) are you constrained to match someone else or is this just for your 
comms?

3) are you going table driven (recommended)?

4) do you know your coefficients?

The basic implementation of a CRC would be no different than in plain 
"C".  Pass in a buffer address and a size like:

// for an 8 bit CRC using standard "C" pointers
- (unsigned char) calculateCRCFromBuffer:(char *) byteBuffer withSize:
(unsigned int) size
{
  do your CRC just like in "C"

  return (CRC);
}

// for a 32 bit CRC using NSData
- (unsigned int) calculateCRC32FromData:(NSData *) myData
{

  int        size = [NSData length];

  int        *buffer = (int *)[myData bytes];

  int        CRC definitions;

  //
  // check to see if myData is valid.  Note: it is valid to message 
nil objects above
  // and also verify 32 bit CRC has 32 bit data.  This might or not 
be important in
  // your application
  //
  if (myData && ((size & 3) == 0))
  {
      do your CRC just like in "C"
  }

  return (CRC);
}

You could also simply call a standard "C" routine since Objective-C is 
a full superset of "C".

The tricky part is there is no standard CRC.  A CRC will change from 
application to application.  It might be 8 bit, 12 bit, 16 bit, 24 
bit, 32 bit or even 64 bit.  Coefficients used are not standard. 
Sometimes, the results might be a 1's complement.  Both bit and byte 
Endian order is also significant.  This is not an issue if you only 
have to match your self but if you are trying to satisfy a specific 
protocol (such as CAN bus), you are best off simply using the sample 
code/algorithm provided by specification.

Steven

Related mailsAuthorDate
mllooking for a crc code Angelo Chen Jun 14, 06:34
mlRe: looking for a crc code Graham Cox Jun 14, 06:39
mlRe: looking for a crc code Ilan Volow Jun 14, 13:59
mlRe: looking for a crc code Jens Alfke Jun 14, 17:25
mlRe: looking for a crc code Steven Noyes Jun 14, 18:55
mlRe: looking for a crc code Andrew Farmer Jun 14, 20:30
mlRe: looking for a crc code Michael Hall Jun 14, 21:32
mlRe: looking for a crc code Michael Vannorsdel Jun 15, 02:04
mlRe: looking for a crc code Jason Coco Jun 15, 09:19
mlRe: looking for a crc code Andreas Mayer Jun 15, 10:59
mlRe: looking for a crc code Ilan Volow Jun 15, 23:09
mlRe: looking for a crc code Chris Suter Jun 16, 02:23
mlRe: looking for a crc code Jens Alfke Jun 16, 20:27