Last active
December 29, 2015 05:39
-
-
Save leighmcculloch/7623204 to your computer and use it in GitHub Desktop.
A category for NSData that returns a hex string of the data within. Output is like: af459a2f
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
// | |
// NSData+HexString.h | |
// | |
// Copyright (c) 2013, Leigh McCulloch. All rights reserved. | |
// BSD-2-Clause License: http://opensource.org/licenses/BSD-2-Clause | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSData (HexString) | |
- (NSString *)hexString; | |
@end |
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
// | |
// NSData+HexString.m | |
// | |
// Copyright (c) 2013, Leigh McCulloch. All rights reserved. | |
// BSD-2-Clause License: http://opensource.org/licenses/BSD-2-Clause | |
// | |
#import "NSData+HexString.h" | |
@implementation NSData (HexString) | |
- (NSString *)hexString { | |
NSMutableString *hex = [NSMutableString stringWithCapacity:[self length]*2]; | |
[self enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) { | |
const unsigned char *dataBytes = (const unsigned char *)bytes; | |
for (NSUInteger i = byteRange.location; i < byteRange.length; ++i) { | |
[hex appendFormat:@"%02x", dataBytes[i]]; | |
} | |
}]; | |
return hex; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment