It looks like you're new here. If you want to get involved, click one of these buttons!
@interface NSData (NSData_HexAdditions)
- (NSString*) stringWithHexBytes1;
- (NSString*) stringWithHexBytes2;
@end
@implementation NSData (NSData_HexAdditions)
- (NSString*) stringWithHexBytes {
NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 2)];
const unsigned char *dataBuffer = [self bytes];
int i;
for (i = 0; i < [self length]; ++i) {
[stringBuffer appendFormat:@"%02X", (unsigned long)dataBuffer[i]];
}
return [[stringBuffer copy] autorelease];
}
- (NSString*)stringWithHexBytesss {
static const char hexdigits[] = "0123456789ABCDEF";
const size_t numBytes = [self length];
const unsigned char* bytes = [self bytes];
char *strbuf = (char *)malloc(numBytes * 2 + 1);
char *hex = strbuf;
NSString *hexBytes = nil;
for (int i = 0; i<numBytes; ++i) {
const unsigned char c = *bytes++;
*hex++ = hexdigits[(c >> 4) & 0xF];
*hex++ = hexdigits[(c ) & 0xF];
}
*hex = 0;
hexBytes = [NSString stringWithUTF8String:strbuf];
free(strbuf);
return hexBytes;
}
@end
And I use it so:
[publicKey stringWithHexBytes1]
[publicKey stringWithHexBytes2]