Created
October 19, 2010 03:01
-
-
Save cqa02303/633534 to your computer and use it in GitHub Desktop.
ついカッとなって勢いで書いた奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString*)base64:(NSData*)fromData { | |
static unsigned char base64Table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
const unsigned char *data = fromData.bytes; | |
char *buff = NSZoneMalloc(self.zone, fromData.length * 4 / 3 + 4); | |
int o = 0, i; | |
for (i = 0; i <= fromData.length; i++, o++) { | |
switch (i % 3) { | |
case 0: | |
if (i < fromData.length) { | |
buff[o] = base64Table[(data[i] >> 2) & 0x3f]; | |
} | |
break; | |
case 1: | |
buff[o] = base64Table[((data[i - 1] << 4) | ((i < fromData.length ? data[i] : 0) >> 4)) & 0x3f]; | |
break; | |
case 2: | |
buff[o] = base64Table[((data[i - 1] << 2) | ((i < fromData.length ? data[i] : 0) >> 6)) & 0x3f]; | |
if (i < fromData.length) { | |
o++; | |
buff[o] = base64Table[data[i] & 0x3f]; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
buff[o] = '\0'; | |
NSString *ret = [NSString stringWithCString:buff encoding:NSASCIIStringEncoding]; | |
NSZoneFree(self.zone, buff); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment