crc for download while using NSURLConnection
I have implemented downloading part using NSURLC开发者_开发技巧onnection. Now I am trying to add one more function for CRC check. I read some basic concepts through wikipedia. But I am bit confused how to start off. Can anyone give me hint for this? Thank you
btw.. I am not trying to design the whole crc part by myself.
zlib, which is included with iOS, includes a crc32() function:
uLong crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, [self bytes], (uInt)[self length]);
return crc;
Note that as Snips comments below, CRCs are built into TCP/IP. There are, of course, other reasons you might to do a CRC on iOS… but a basic check for packet integrity probably isn't one of them.
If you prefer to implement simple crc by yourself. This can help.
For example adopted BSD 16-bit crc algorithm:
- (NSInteger)getBSDcrc:(NSData*)data {
NSUInteger lendth = [data length];
char *byte = (char*)[data bytes];
NSInteger crc = 0;
for (int i = 0; i < length; ++i) {
crc = (crc >> 1) + ((crc & 1) << 15);
crc += byte[i];
crc &= 0xffff;
}
return crc;
}
精彩评论