objective c - NSString (hex) to bytes -
is there method in objective-c converts hex string bytes? example @"1156ffcd3430aa22"
unsigned char array {0x11, 0x56, 0xff, ...}
.
fastest nsstring category implementation think of (cocktail of examples):
- (nsdata *)datafromhexstring { const char *chars = [self utf8string]; int = 0, len = self.length; nsmutabledata *data = [nsmutabledata datawithcapacity:len / 2]; char bytechars[3] = {'\0','\0','\0'}; unsigned long wholebyte; while (i < len) { bytechars[0] = chars[i++]; bytechars[1] = chars[i++]; wholebyte = strtoul(bytechars, null, 16); [data appendbytes:&wholebyte length:1]; } return data; }
it close 8 times faster wookay's solution. nsscanner quite slow.
Comments
Post a Comment