NSData+FastHex adds a category on NSData
to convert to and from a hexadecimal
string representation. As the name implies, it has a focus on performance,
without sacrificing code clarity.
Other implementations found online perform multiple message sends per byte and make additional copies of data buffers, which is wasteful.
CFStringInlineBuffer
for efficient character access (fast enumeration of string characters)-[NSData enumerateByteRangesUsingBlock:]
to avoid an extra copy of the NSData buffer if the data isn't contiguous-initWithBytesNoCopy:…
to avoid an extra copy of the encoded and decoded data and string buffers
#import "NSData+FastHex.h"
uint8_t bytes[] = {0xDE, 0xAD, 0xBE, 0xEF, 0x42};
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSString *hexString = [data hexStringRepresentation]; // => @"DEADBEEF42"
NSData *decoded = [NSData dataWithHexString:hexString];
var bytes: [UInt8] = [0xDE, 0xAD, 0xBE, 0xEF, 0x42]
var data = NSData(bytes: bytes, length: bytes.count)
var hexString = data.hexStringRepresentation() // => "DEADBEEF42"
var decoded = NSData(hexString: hexString)
NSData+FastHex is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "NSData+FastHex"
Jonathon Mah, [email protected]
NSData+FastHex is available under the MIT license. See the LICENSE file for more info.