与某一个值异或校验
/**
@param contentData 需要校验的内容
@return 异或值
*/
- (int)contentCheckValue:(NSData *)contentData {
Byte *testByte = (Byte *)[contentData bytes];
int checksum = 0;
for(int i=0; i<[contentData length]; i++) {
checksum ^= testByte[i];
}
return checksum;
}
与一个固定的值(0x5A)异或
/**
与一个固定的值异或
@return 异或后的值
*/
- (NSData *)xor_0X5A {
NSMutableData *data = [NSMutableData dataWithLength:1];
uint8_t num = 0x5A;
[data replaceBytesInRange:NSMakeRange(0, 1) withBytes:&num];
Byte *byte1 = (Byte *)[self.copy bytes];
Byte *byte2 = (Byte *)[data bytes];
for(int i = 0; i < self.length; i++) {
byte1[i] ^= byte2[0];
}
NSData *data1 = [[NSData alloc] initWithBytes:byte1 length:self.length];
return data1;
}
异或校验(每一字节分别异或)
/**
异或校验(每一字节分别异或)
@return 校验值
*/
- (int)contentCheckValue {
Byte *testByte = (Byte *)[self bytes];
int checksum = 0;
for(int i=0; i<[self length]; i++) {
checksum ^= testByte[i];
}
return checksum;
}
网友评论