Skip to content

Instantly share code, notes, and snippets.

@satococoa
Created September 1, 2014 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satococoa/b1f4601dc6c34da7d2d2 to your computer and use it in GitHub Desktop.
Save satococoa/b1f4601dc6c34da7d2d2 to your computer and use it in GitHub Desktop.
NSData を 16進数の文字列で表現する
// http://stackoverflow.com/a/9084784
#import <Foundation/Foundation.h>
@interface NSData (NSData_Conversion)
#pragma mark - String Conversion
- (NSString *)hexadecimalString;
@end
@implementation NSData (NSData_Conversion)
#pragma mark - String Conversion
- (NSString *)hexadecimalString {
/* Returns hexadecimal string of NSData. Empty string if data is empty. */
const unsigned char *dataBuffer = (const unsigned char *)[self bytes];
if (!dataBuffer)
return [NSString string];
NSUInteger dataLength = [self length];
NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for (int i = 0; i < dataLength; ++i)
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
return [NSString stringWithString:hexString];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment