@implementation NSData(Hex) -(NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces { const unsigned char* bytes = (const unsigned char*)[self bytes]; NSUInteger nbBytes = [self length]; //If spaces is true, insert a space every this many input bytes (twice this many output characters). static const NSUInteger spaceEveryThisManyBytes = 4UL; //If spaces is true, insert a line-break instead of a space every this many spaces. static const NSUInteger lineBreakEveryThisManySpaces = 4UL; const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces; NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0); NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen]; for(NSUInteger i=0; i