• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Convert hex values in an NSData object to NSString?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Convert hex values in an NSData object to NSString? (From: Dave Batton <email@hidden>)

  • Prev by Date: Re: Leopard NSTableView Cell and single click editing
  • Next by Date: View object not accessible from controller object
  • Previous by thread: Re: Convert hex values in an NSData object to NSString?
  • Next by thread: Anyone having problems with NSArchiver/NSUnarchiver on 10.5.1?
  • Index(es):
    • Date
    • Thread