Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created June 4, 2011 18:57
Show Gist options
  • Save unixpickle/1008200 to your computer and use it in GitHub Desktop.
Save unixpickle/1008200 to your computer and use it in GitHub Desktop.
NSData+hex
//
// 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
//
// 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
@hanspinckaers
Copy link

Thanks! Much more reliable than using NSData's description method to get the stringValue.

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