iOS 十六进制加法
iOS hex addition
我有一个十六进制值的 NSString
NSString* someString = @"AAB827EB5A6E225CAA
我想从 b(第二个字符)提取到 2(-5 个字符)
添加所有提取的字符,我必须找到 5C 作为结果(-4 和 -3 字符)
我试过这个:
NSMutableArray *hex = [[NSMutableArray alloc]init];
unichar firstChar = [[someString uppercaseString] characterAtIndex:0];
unichar seconChar = [[someString uppercaseString] characterAtIndex:1];
unichar lastChar = [[someString uppercaseString] characterAtIndex:[print length]-1];
unichar beforeLastChar = [[someString uppercaseString] characterAtIndex:[print length]-2];
if (firstChar == 'A' && seconChar == 'A' && lastChar =='A' && beforeLastChar=='A') {
for (int i=2;i< [print length]-4; i++) {
NSString *decim =[NSString stringWithFormat:@"%hu",[someString characterAtIndex:i]];
[hex addObject:decim];
}
NSLog(@"hex : %@",hex);
}
但是日志是
hex : (
98,
56,
50,
55,
101,
98,
53,
97,
54,
101,
50,
50, )
我试图将其转换为字符串然后转换为 int 以进行计算,但如果我可以避免转换并继续使用十六进制,我更愿意
感谢帮助
代码可能会进一步简化,但有一种可能性:
NSString *someString = @"AAB827EB5A6E225CAA";
// I have improved a bit your check for prefix and suffix
if ([someString hasPrefix:@"AA"] && [someString hasSuffix:@"AA"]) {
NSMutableArray *hexNumbers = [[NSMutableArray alloc] init];
for (int i = 2; i < [someString length] - 4; i++) {
unichar digit = [someString characterAtIndex:i];
NSUInteger value;
// we have to convert the character into its numeric value
// we could also use NSScanner for it but this is a simple way
if (digit >= 'A') {
value = digit - 'A' + 10;
} else {
value = digit - '0';
}
// add the value to the array
[hexNumbers addObject:@(value)];
}
NSLog(@"hex : %@", hexNumbers);
// a trick to get the sum of an array
NSNumber *sum = [hexNumbers valueForKeyPath:@"@sum.self"];
// print the sum in decadic and in hexadecimal
NSLog(@"Sum: %@, in hexa: %X", sum, [sum integerValue]);
}
我有一个十六进制值的 NSString
NSString* someString = @"AAB827EB5A6E225CAA
我想从 b(第二个字符)提取到 2(-5 个字符)
添加所有提取的字符,我必须找到 5C 作为结果(-4 和 -3 字符)
我试过这个:
NSMutableArray *hex = [[NSMutableArray alloc]init];
unichar firstChar = [[someString uppercaseString] characterAtIndex:0];
unichar seconChar = [[someString uppercaseString] characterAtIndex:1];
unichar lastChar = [[someString uppercaseString] characterAtIndex:[print length]-1];
unichar beforeLastChar = [[someString uppercaseString] characterAtIndex:[print length]-2];
if (firstChar == 'A' && seconChar == 'A' && lastChar =='A' && beforeLastChar=='A') {
for (int i=2;i< [print length]-4; i++) {
NSString *decim =[NSString stringWithFormat:@"%hu",[someString characterAtIndex:i]];
[hex addObject:decim];
}
NSLog(@"hex : %@",hex);
}
但是日志是
hex : ( 98, 56, 50, 55, 101, 98, 53, 97, 54, 101, 50, 50, )
我试图将其转换为字符串然后转换为 int 以进行计算,但如果我可以避免转换并继续使用十六进制,我更愿意
感谢帮助
代码可能会进一步简化,但有一种可能性:
NSString *someString = @"AAB827EB5A6E225CAA";
// I have improved a bit your check for prefix and suffix
if ([someString hasPrefix:@"AA"] && [someString hasSuffix:@"AA"]) {
NSMutableArray *hexNumbers = [[NSMutableArray alloc] init];
for (int i = 2; i < [someString length] - 4; i++) {
unichar digit = [someString characterAtIndex:i];
NSUInteger value;
// we have to convert the character into its numeric value
// we could also use NSScanner for it but this is a simple way
if (digit >= 'A') {
value = digit - 'A' + 10;
} else {
value = digit - '0';
}
// add the value to the array
[hexNumbers addObject:@(value)];
}
NSLog(@"hex : %@", hexNumbers);
// a trick to get the sum of an array
NSNumber *sum = [hexNumbers valueForKeyPath:@"@sum.self"];
// print the sum in decadic and in hexadecimal
NSLog(@"Sum: %@, in hexa: %X", sum, [sum integerValue]);
}