-
Notifications
You must be signed in to change notification settings - Fork 3
/
NSData+hexDigits.m
45 lines (39 loc) · 1.42 KB
/
NSData+hexDigits.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// NSData+hexDigits.m
// AquaticPrime Developer
//
// Created by Geode on 07/09/2012.
//
//
#import "NSData+hexDigits.h"
@implementation NSData (hexDigits)
+ (NSData*)MCC_PREFIXED_NAME(dataWithHexDigitRepresentation):(NSString*)hexDigitString {
NSUInteger textLength = [hexDigitString length];
NSAssert(textLength % 2 == 0, @"Expected an even count of hex digits");
NSMutableData *data = [NSMutableData dataWithCapacity:textLength/2];
NSRange range = NSMakeRange(0, 2);
unsigned int uintValue = 0;
uint8_t byteValue = 0;
for (range.location = 0; range.location < textLength; range.location+=2) {
NSScanner *scanner = [NSScanner scannerWithString:[hexDigitString substringWithRange:range]];
if ([scanner scanHexInt:&uintValue]) {
byteValue = (uint8_t)uintValue;
[data appendBytes:&byteValue length:1];
}
else {
// Failed to convert (bad hex digit?)
return nil;
}
}
return data;
}
- (NSString*)MCC_PREFIXED_NAME(hexDigitRepresentation) {
uint8_t *rawBytes = (uint8_t*)[self bytes];
NSUInteger byteIndex = 0, byteLength = [self length];
NSMutableString *hexRepresentation = [NSMutableString stringWithCapacity:byteLength*2];
for (byteIndex = 0; byteIndex < byteLength; byteIndex++) {
[hexRepresentation appendFormat:@"%02x", rawBytes[byteIndex]];
}
return hexRepresentation;
}
@end