Skip to content

Instantly share code, notes, and snippets.

@fcanas
Last active September 2, 2015 14:12
Show Gist options
  • Save fcanas/b8f1900b3525af1a12b7 to your computer and use it in GitHub Desktop.
Save fcanas/b8f1900b3525af1a12b7 to your computer and use it in GitHub Desktop.
Getting a hex string out of NSData
@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
@fcanas
Copy link
Author

fcanas commented Sep 1, 2015

The use of enumerateByteRangesUsingBlock: keeps this moderately efficient for non-contiguous NSData, and shouldn't have an impact on contiguous NSData.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment