• 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: Strange malloc/free issue inside a category
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange malloc/free issue inside a category


  • Subject: Re: Strange malloc/free issue inside a category
  • From: Ken Tozier <email@hidden>
  • Date: Sun, 30 Jul 2006 22:36:34 -0400

FWIW, here are the hex encoder/decoder categories. I plan on looking into Shawn E's base64 suggestion as well, but this (or an adaptation of it) might prove useful to someone else.

Ken

@implementation NSData (Hex_Extensions)

- (NSString *) unicharHexStringFromData
{
int slen = [self length],
dlen = slen * 2;

unsigned char *ss = (unsigned char *) [self bytes],
*se = ss + slen;

unichar *dbuf = (unichar *) malloc (dlen * sizeof(unichar)),
*ds = dbuf,
ch;

while (ss < se)
{
ch = (*ss >> 4);
*ds = (ch < 0x0a) ? 0x30 + ch : 0x57 + ch ;
ds++;

ch = (*ss & 0x0f);
*ds = (ch < 0x0a) ? 0x30 + ch : 0x57 + ch ;
ds++;

ss++;
}

NSString *result = [NSString stringWithCharacters: dbuf length: dlen];

free(dbuf);

return result;
}


@end


@implementation NSString (Hex_Extensions)

- (NSData *) dataFromUnicharHexString
{
	int					slen	= [self length],
						dlen	= slen / 2;

	unichar				*sbuf	= (unichar *) malloc(slen * sizeof(unichar)),
						*ss		= sbuf,
						*se		= ss + slen;

	unsigned char		*dbuf	= (unsigned char *) malloc(dlen),
						*ds		= dbuf,
						ch;

	[self getCharacters: sbuf];

	while (ss < se)
	{
		ch		 =	(*ss < 0x3a) ? (*ss - 0x30) << 4 : (*ss - 0x57) << 4 ;
		ss++;
		ch		|=	(*ss < 0x3a) ? *ss - 0x30 : *ss - 0x57 ;
		ss++;

		*ds		 = ch;
		ds++;
	}

	NSData				*result		= [NSData dataWithBytes: dbuf length: dlen];

	free(sbuf);
	free(dbuf);

	return result;
}


@end _______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Strange malloc/free issue inside a category (From: Ken Tozier <email@hidden>)
 >Re: Strange malloc/free issue inside a category (From: Shawn Erickson <email@hidden>)

  • Prev by Date: NSControl/NSCell: archiving vs. bindings and target/action
  • Next by Date: Re: Making Cocoa code available for AppleScripters
  • Previous by thread: Re: Strange malloc/free issue inside a category
  • Next by thread: Re: Strange malloc/free issue inside a category
  • Index(es):
    • Date
    • Thread