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
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to