Created
September 1, 2014 06:19
-
-
Save satococoa/b1f4601dc6c34da7d2d2 to your computer and use it in GitHub Desktop.
NSData を 16進数の文字列で表現する
This file contains hidden or 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
// 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