FROM : stephen joseph butler
DATE : Thu Nov 29 23:43:15 2007
On Nov 29, 2007 12:27 PM, Dave Batton <<email_removed>> wrote:
> I'm using Andreas Mayer's NSData_AMDigest category to get an MD5
> digest. It returns an NSData object, but what I need now is an
> NSString of the hex characters.
>
> What's the best way to do this? Sorry, I'm an interface guy, and not
> very good at twiddling bits. Any help would be appreciated.
I'd probably just write a quick category on NSData. Something like
this would work nicely:
#import <Foundation/Foundation.h>
@interface NSData (NSData_HexAdditions)
- (NSString*) stringWithHexBytes;
@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];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog( @"Hex String = %@", [[@"Here is a test!\n"
dataUsingEncoding:NSUTF16StringEncoding] stringWithHexBytes] );
[pool release];
return 0;
}
DATE : Thu Nov 29 23:43:15 2007
On Nov 29, 2007 12:27 PM, Dave Batton <<email_removed>> wrote:
> I'm using Andreas Mayer's NSData_AMDigest category to get an MD5
> digest. It returns an NSData object, but what I need now is an
> NSString of the hex characters.
>
> What's the best way to do this? Sorry, I'm an interface guy, and not
> very good at twiddling bits. Any help would be appreciated.
I'd probably just write a quick category on NSData. Something like
this would work nicely:
#import <Foundation/Foundation.h>
@interface NSData (NSData_HexAdditions)
- (NSString*) stringWithHexBytes;
@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];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog( @"Hex String = %@", [[@"Here is a test!\n"
dataUsingEncoding:NSUTF16StringEncoding] stringWithHexBytes] );
[pool release];
return 0;
}
| Related mails | Author | Date |
|---|---|---|
| Dave Batton | Nov 29, 19:27 | |
| Nick Zitzmann | Nov 29, 23:19 | |
| stephen joseph but… | Nov 29, 23:43 |






Cocoa mail archive

