您必須首先將每個字符轉換爲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