- (NSData *)dataWithHexString:(NSString *)hexString {
NSInteger len = [hexString length];
char *myBuffer = (char *)malloc(len / 2 + 1);
bzero(myBuffer, len / 2 + 1);
for (int i = 0; i < len - 1; i += 2) {
unsigned int anInt;
NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
NSScanner * scanner = [NSScanner scannerWithString:hexCharStr] ;
[scanner scanHexInt:&anInt];
myBuffer[i / 2] = (char)anInt;
}
NSData *hexData = [[NSData alloc] initWithBytes:myBuffer length:len/2];
free(myBuffer);
return hexData;
}
网友评论