allenforrest
2015-12-14 12:29:01 +08:00
```
- (NSData *)dataFromHexString {
const char *chars = [self UTF8String];
NSUInteger i = 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;
}
```
拿走不谢。