您必须首先将每个字符转换为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;
}
希望它可以帮助的字符实用方法!
超棒的家伙,这是该死的超级,正是我想要的再次感谢!!!!! – Satheesh 2012-07-11 07:01:00
@Satheesh Thanx! – 2012-07-11 08:09:42
嗨朋友你有什么想法我的问题http://stackoverflow.com/questions/11481782/how-to-calculate-crc-16-from-hex-values – Satheesh 2012-07-14 10:00:17