6

可能重复:
在 Objective-C 中获取 NSData 的 CRC 校验和

我在 xcode 中找不到任何 CRC32 算法的实现。谁能帮我计算一下?

4

1 回答 1

16

libz has a crc32() function. To use it with NSData, try this simple category:

Your header:

@interface NSData (CRC32)
- (uint32_t)CRC32Value;
@end

Your implementation:

#include "your header"

#include <zlib.h>

@implementation NSData (CRC32)
- (uint32_t)CRC32Value {
    uLong crc = crc32(0L, Z_NULL, 0);
    crc = crc32(crc, [self bytes], [self length]);
    return crc;
}
@end
于 2011-04-02T18:00:48.453 回答