异或,英文为exclusive OR,或缩写成xor
使用:
NSString *str = [ViewController pinxCreator:@”24″ withPinv:@”23″];
NSLog(@”—%@”,str);
NSString *str1 = [ViewController pinxCreator:str withPinv:@”23″];
NSLog(@”—%@”,str1);
Log:
2016-03-30 11:41:11.145 testpp[2132:164780] —07
2016-03-30 11:41:11.145 testpp[2132:164780] —24
代码:
+ (NSString *)pinxCreator:(NSString *)pan withPinv:(NSString *)pinv
{
if (pan.length != pinv.length)
{
return nil;
}
const char *panchar = [pan UTF8String];
const char *pinvchar = [pinv UTF8String];
NSString *temp = [[NSString alloc] init];
for (int i = 0; i < pan.length; i++)
{
int panValue = [self charToint:panchar[i]];
int pinvValue = [self charToint:pinvchar[i]];
temp = [temp stringByAppendingString:[NSString stringWithFormat:@”%X”,panValue^pinvValue]];
}
return temp;
}
+ (int)charToint:(char)tempChar
{
if (tempChar >= ‘0’ && tempChar <=’9′)
{
return tempChar – ‘0’;
}
else if (tempChar >= ‘A’ && tempChar <= ‘F’)
{
return tempChar – ‘A’ + 10;
}
return 0;
}
你必须 登录后 才能对文章进行评论!