您必须首先将每个字符转换为 Base16(十六进制)格式。然后您应该对这些字符进行异或运算。您可以使用 strtol() 函数来实现此目的。
NSString *hex1 = @"50be4f3de4";
NSString *hex2 = @"30bf69a299";
NSMutableArray *hexArray1 = [self splitStringIntoChars:hex1];
NSMutableArray *hexArray2 = [self splitStringIntoChars:hex2];
NSMutableString *str = [NSMutableString new];
for (int i=0; i<[hexArray1 count]; i++ )
{
/*Convert to base 16*/
int a=(unsigned char)strtol([[hexArray1 objectAtIndex:i] UTF8String], NULL, 16);
int b=(unsigned char)strtol([[hexArray2 objectAtIndex:i] UTF8String], NULL, 16);
char encrypted = a ^ b;
NSLog(@"%x",encrypted);
[str appendFormat:@"%x",encrypted];
}
NSLog(@"%@",str);
我用来分割字符串字符的实用方法
-(NSMutableArray*)splitStringIntoChars:(NSString*)argStr{
NSMutableArray *characters = [[NSMutableArray alloc]
initWithCapacity:[argStr length]];
for (int i=0; i < [argStr length]; i++)
{
NSString *ichar = [NSString stringWithFormat:@"%c", [argStr characterAtIndex:i ]];
[characters addObject:ichar];
}
return characters;
}
希望能帮助到你!!