Re: looking for a crc code
Re: looking for a crc code
- Subject: Re: looking for a crc code
- From: Steven Noyes <email@hidden>
- Date: Sat, 14 Jun 2008 11:55:49 -0500
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 (email@hidden)
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:
This email sent to email@hidden