3 // libsecurity_transform
5 // Copyright (c) 2011,2014 Apple Inc. All Rights Reserved.
8 #import "NSData+HexString.h"
10 @implementation NSData (HexString)
13 +(id)dataWithHexString:(NSString *)hex
17 NSAssert(0 == [hex length] % 2, @"Hex strings should have an even number of digits (%@)", hex);
18 unsigned char *bytes = malloc([hex length]/2);
19 unsigned char *bp = bytes;
20 for (CFIndex i = 0; i < [hex length]; i += 2) {
21 buf[0] = [hex characterAtIndex:i];
22 buf[1] = [hex characterAtIndex:i+1];
24 *bp++ = strtol(buf, &b2, 16);
25 NSAssert(b2 == buf + 2, @"String should be all hex digits: %@ (bad digit around %ld)", hex, i);
28 return [NSData dataWithBytesNoCopy:bytes length:[hex length]/2 freeWhenDone:YES];