#pragma mark--CRC8校验
+ (NSString *)crc8_maxin_byteCheckWithHexString:(NSString*)hexString {
NSString * tempStr = hexString;
NSArray *tempArray = [self getByteForString:hexString];
// NSArray * tempArray = [tempStr componentsSeparatedByString:@" "];//分隔符
unsigned char testChars[(int)tempArray.count];
for(int i=0;i<tempArray.count;i++){
NSString * string = tempArray[i];
unsigned char fristChar = [self hexHighFromChar:[string characterAtIndex:0]];
unsigned char lastChar = [self hexLowFromChar:[string characterAtIndex:1]];
unsigned char temp = fristChar+lastChar;
testChars[i] = temp;
}
unsigned char res = [self crc8_maxin_checkWithChars:testChars length:(int)tempArray.count];
return [NSString stringWithFormat:@"%x", res];
}
+(unsigned char)hexHighFromChar:(unsigned char) tempChar{
unsigned char temp = 0x00;
switch (tempChar) {
case 'a':temp = 0xa0;break;
case 'A':temp = 0xA0;break;
case 'b':temp = 0xb0;break;
case 'B':temp = 0xB0;break;
case 'c':temp = 0xc0;break;
case 'C':temp = 0xC0;break;
case 'd':temp = 0xd0;break;
case 'D':temp = 0xD0;break;
case 'e':temp = 0xe0;break;
case 'E':temp = 0xE0;break;
case 'f':temp = 0xf0;break;
case 'F':temp = 0xF0;break;
case '1':temp = 0x10;break;
case '2':temp = 0x20;break;
case '3':temp = 0x30;break;
case '4':temp = 0x40;break;
case '5':temp = 0x50;break;
case '6':temp = 0x60;break;
case '7':temp = 0x70;break;
case '8':temp = 0x80;break;
case '9':temp = 0x90;break;
default:temp = 0x00;break;
}
return temp;
}
+(unsigned char)hexLowFromChar:(unsigned char) tempChar{
unsigned char temp = 0x00;
switch (tempChar) {
case 'a':temp = 0x0a;break;
case 'A':temp = 0x0A;break;
case 'b':temp = 0x0b;break;
case 'B':temp = 0x0B;break;
case 'c':temp = 0x0c;break;
case 'C':temp = 0x0C;break;
case 'd':temp = 0x0d;break;
case 'D':temp = 0x0D;break;
case 'e':temp = 0x0e;break;
case 'E':temp = 0x0E;break;
case 'f':temp = 0x0f;break;
case 'F':temp = 0x0F;break;
case '1':temp = 0x01;break;
case '2':temp = 0x02;break;
case '3':temp = 0x03;break;
case '4':temp = 0x04;break;
case '5':temp = 0x05;break;
case '6':temp = 0x06;break;
case '7':temp = 0x07;break;
case '8':temp = 0x08;break;
case '9':temp = 0x09;break;
default:temp = 0x00;break;
}
return temp;
}
+(char)crc8_maxin_checkWithChars:(unsigned char *)chars length:(int)len{
unsigned char i;
unsigned char crc=0x00; /* 计算的初始crc值 */
unsigned char *ptr = chars;
while(len--){
crc ^= *ptr++;
for(i = 0;i < 8;i++)
{
if(crc & 0x01){
crc = (crc >> 1) ^ 0x8C;
}else crc >>= 1;
}
}
return crc;
}
+ (NSArray *)getByteForString:(NSString *)string {
NSMutableArray *strArr = [NSMutableArray array];
for (int i = 0; i < string.length/2; i++) {
NSString *str = [string substringWithRange:NSMakeRange(i * 2, 2)];
[strArr addObject:str];
}
return [strArr copy];
}
网友评论