Last active
September 2, 2015 14:12
-
-
Save fcanas/b8f1900b3525af1a12b7 to your computer and use it in GitHub Desktop.
Getting a hex string out of NSData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation NSData (HexString) | |
- (NSString *)hex_hexString { | |
NSMutableString *out = [NSMutableString stringWithCapacity:self.length * 2]; | |
[self enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) { | |
for (NSUInteger i = 0; i < byteRange.length; i++) { | |
[out appendFormat:@"%02x", ((unsigned char *)bytes)[i]]; | |
} | |
}]; | |
return out; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The use of
enumerateByteRangesUsingBlock:
keeps this moderately efficient for non-contiguousNSData
, and shouldn't have an impact on contiguousNSData
.