> Is there any cocoa class which convert NSString or NSData to
> Base64encoding ?
grossly inefficient, but does the job for me...
- (PFString*)base64Representation
{
char *base64array =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
PFString *retval = [PFString stringWithString:@""];
PFAutoreleasePool *pool = [PFAutoreleasePool new];
int bytesLeft = 0;
unsigned char *dataPtr = NULL;
dataPtr = [self bytes];
bytesLeft = [self length];
for(;bytesLeft>0;) {
char to_add[16];
unsigned int dataWord = 0;
int i;
/* make the data word */
for(i=0;i<3;i++) {
dataWord = dataWord << 8;
if(bytesLeft > 0)
dataWord = dataWord | *dataPtr++;
bytesLeft--;
}
/* create the array */
for(i=3;i>=0;i--) {
unsigned int thisIndex = dataWord & 0x3f;
dataWord = dataWord >> 6;
to_add[i] = base64array[thisIndex];
}
/* finish it off */
to_add[4] = '\0';
switch(bytesLeft) {
case -2:
to_add[2] = '=';
case -1:
to_add[3] = '=';
case 0:
break;
}
/* add it */
[retval appendFormat:@"%s", to_add];
}
/* tidy up and release */
[pool release];
return retval;
}
_______________________________________________
Discuss-gnustep mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/discuss-gnustep