Re: Convert hex values in an NSData object to NSString?
Re: Convert hex values in an NSData object to NSString?
- Subject: Re: Convert hex values in an NSData object to NSString?
- From: "stephen joseph butler" <email@hidden>
- Date: Thu, 29 Nov 2007 16:43:15 -0600
On Nov 29, 2007 12:27 PM, Dave Batton <email@hidden> 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:@"X", (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;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden