Skip to content

Instantly share code, notes, and snippets.

@leighmcculloch
Last active December 29, 2015 05:39
Show Gist options
  • Save leighmcculloch/7623204 to your computer and use it in GitHub Desktop.
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
//
// 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
//
// 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