NSString r = @"";
r = [r stringByAppendingString: [ [s substringWithRange:NSRangeMake(i, i + 2)] intValue]];
Or:
NSMutableString r = @"";
[r appendingString: [ [s substringWithRange:NSRangeMake(i, i + 2)] intValue]];
It's just that I believe there are better ways of achieving the same. Can you provide a bit more about what s
contains and what you are about to achieve? e.g. when this is done just to get rid of spaces, then the following would do - even without the for loop.
NSMutableString r = [NSMutableString stringWithString:s];
[r replaceOccurrencesOfString:@" " withString:"" options:NSLiteralSearch range:NSMakeRange(0, [s length])];
Edit in response to your comment.
But be warned. Use this as a starting point only. I never did that myself before. There may be better solutions around and it may contain minor mistakes.
- (NSString) hexStringToByteString(NSString s) {
NSMutableData d = [NSData dataWithLength: [s length] / 2];
NSUInteger v;
for (int i = 0, i < [s length], i+=2) {
NSScanner *scanner = [NSScanner scannerWithString:[s substringWithRange:NSRangeMake(i, i + 2)]];
[scanner scanHexInt:&v];
[d appendBytes:&v length:1];
}
NSData r = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
return r;
}
You may opt for a different encoding of course.