Created
June 4, 2011 18:57
-
-
Save unixpickle/1008200 to your computer and use it in GitHub Desktop.
NSData+hex
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+hex.h | |
// NSData+hex | |
// | |
// Created by Alex Nichol on 3/29/11. | |
// Copyright 2011 Jitsik. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSData (hex) | |
- (NSString *)hexadecimalString; | |
@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+hex.m | |
// Roboto | |
// | |
// Created by Alex Nichol on 3/29/11. | |
// Copyright 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "NSData+hex.h" | |
@implementation NSData (hex) | |
- (NSString *)hexadecimalString { | |
NSMutableString * hexString = [[NSMutableString alloc] init]; | |
const unsigned char * bytes = [self bytes]; | |
for (int i = 0; i < [self length]; i++) { | |
[hexString appendFormat:@"%02X", bytes[i]]; | |
} | |
NSString * immutableHex = [NSString stringWithString:hexString]; | |
[hexString release]; | |
return immutableHex; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Much more reliable than using NSData's description method to get the stringValue.