Objective-C版Base64
2015-09-20 18:38
387 查看
#define xx 65
static char exchargeTable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char base64DecodeLookup[256] = {
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, 62,xx,
xx, xx,
63,
52, 53,54,
55, 56,
57, 58,59,
60, 61,
xx, xx,xx,
xx, xx,
xx,
xx, 0, 1,
2, 3,
4, 5, 6,
7, 8,
9, 10,11,
12, 13,
14,
15, 16,17,
18, 19,
20, 21,22,
23, 24,
25, xx,xx,
xx, xx,
xx,
xx, 26,27,
28, 29,
30, 31,32,
33, 34,
35, 36,37,
38, 39,
40,
41, 42,43,
44, 45,
46, 47,48,
49, 50,
51, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
};
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation NSString (Base64)
- (NSString*)base64Encode{
NSString *ret = nil;
NSMutableString
*codeString = [NSMutableStringstring];
int lenMod = [selflength]%3;
for (int i =0; i <(int)([selflength]/3) ; i++) {
int index = i *3;
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:index]>>2]];
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:index]&0x03)<<4)|(([selfcharacterAtIndex:index+1]&0xF0)>>4)]];
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:index+1]&0x0F)<<2)|([selfcharacterAtIndex:index+2]>>6)]];
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:index+2]&0x3F]];
}
if (lenMod != 0) {
int startIndex = ((int)[selflength])/3 *3;
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:startIndex]>>2]];
if (lenMod > 1) {
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:startIndex]&0x03)<<4)|(([selfcharacterAtIndex:startIndex+1]&0xF0)>>4)]];
[codeString appendFormat:@"%c",exchargeTable[([selfcharacterAtIndex:startIndex+1]&0x0F)<<2]];
}else{
[codeString appendFormat:@"%c",exchargeTable[([selfcharacterAtIndex:startIndex]&0x03)<<4]];
[codeString appendFormat:@"%c",'='];
}
[codeString appendFormat:@"%c",'='];
}
ret = [NSStringstringWithString:codeString];
return ret;
}
- (NSString*)base64Decode{
NSString *ret = nil;
NSMutableString
*decodeString = [NSMutableStringstring];
int lenMod = [selflength]%4;
if (lenMod == 0) {
for (int i =0; i < (int)([selflength]/4); i++) {
int index = i*4;
int char1Pos = base64DecodeLookup[[selfcharacterAtIndex:index]];
int char2Pos = base64DecodeLookup[[selfcharacterAtIndex:index+1]];
int char3Pos = base64DecodeLookup[[selfcharacterAtIndex:index+2]];
int char4Pos = base64DecodeLookup[[selfcharacterAtIndex:index+3]];
[decodeString appendFormat:@"%c",((char1Pos<<2)&0xFC)|((char2Pos&0x30)>>4)];
if ([selfcharacterAtIndex:index+2] !='=') {
[decodeString appendFormat:@"%c",((char2Pos&0x0F)<<4)|((char3Pos>>2)&0x0F)];
if ([selfcharacterAtIndex:index+3] =='=') {
[decodeString appendFormat:@"%c",(char3Pos&0x03)<<6];
}else{
[decodeString appendFormat:@"%c",((char3Pos&0x03)<<6)|(char4Pos&0x3F)];
}
}else{
[decodeString appendFormat:@"%c",(char2Pos&0x0F)<<4];
}
}
}
ret = [NSStringstringWithString:decodeString];
return ret;
}
@end
static char exchargeTable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char base64DecodeLookup[256] = {
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, 62,xx,
xx, xx,
63,
52, 53,54,
55, 56,
57, 58,59,
60, 61,
xx, xx,xx,
xx, xx,
xx,
xx, 0, 1,
2, 3,
4, 5, 6,
7, 8,
9, 10,11,
12, 13,
14,
15, 16,17,
18, 19,
20, 21,22,
23, 24,
25, xx,xx,
xx, xx,
xx,
xx, 26,27,
28, 29,
30, 31,32,
33, 34,
35, 36,37,
38, 39,
40,
41, 42,43,
44, 45,
46, 47,48,
49, 50,
51, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx, xx,xx,
xx, xx,
xx,
};
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation NSString (Base64)
- (NSString*)base64Encode{
NSString *ret = nil;
NSMutableString
*codeString = [NSMutableStringstring];
int lenMod = [selflength]%3;
for (int i =0; i <(int)([selflength]/3) ; i++) {
int index = i *3;
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:index]>>2]];
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:index]&0x03)<<4)|(([selfcharacterAtIndex:index+1]&0xF0)>>4)]];
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:index+1]&0x0F)<<2)|([selfcharacterAtIndex:index+2]>>6)]];
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:index+2]&0x3F]];
}
if (lenMod != 0) {
int startIndex = ((int)[selflength])/3 *3;
[codeString appendFormat:@"%c",exchargeTable[[selfcharacterAtIndex:startIndex]>>2]];
if (lenMod > 1) {
[codeString appendFormat:@"%c",exchargeTable[(([selfcharacterAtIndex:startIndex]&0x03)<<4)|(([selfcharacterAtIndex:startIndex+1]&0xF0)>>4)]];
[codeString appendFormat:@"%c",exchargeTable[([selfcharacterAtIndex:startIndex+1]&0x0F)<<2]];
}else{
[codeString appendFormat:@"%c",exchargeTable[([selfcharacterAtIndex:startIndex]&0x03)<<4]];
[codeString appendFormat:@"%c",'='];
}
[codeString appendFormat:@"%c",'='];
}
ret = [NSStringstringWithString:codeString];
return ret;
}
- (NSString*)base64Decode{
NSString *ret = nil;
NSMutableString
*decodeString = [NSMutableStringstring];
int lenMod = [selflength]%4;
if (lenMod == 0) {
for (int i =0; i < (int)([selflength]/4); i++) {
int index = i*4;
int char1Pos = base64DecodeLookup[[selfcharacterAtIndex:index]];
int char2Pos = base64DecodeLookup[[selfcharacterAtIndex:index+1]];
int char3Pos = base64DecodeLookup[[selfcharacterAtIndex:index+2]];
int char4Pos = base64DecodeLookup[[selfcharacterAtIndex:index+3]];
[decodeString appendFormat:@"%c",((char1Pos<<2)&0xFC)|((char2Pos&0x30)>>4)];
if ([selfcharacterAtIndex:index+2] !='=') {
[decodeString appendFormat:@"%c",((char2Pos&0x0F)<<4)|((char3Pos>>2)&0x0F)];
if ([selfcharacterAtIndex:index+3] =='=') {
[decodeString appendFormat:@"%c",(char3Pos&0x03)<<6];
}else{
[decodeString appendFormat:@"%c",((char3Pos&0x03)<<6)|(char4Pos&0x3F)];
}
}else{
[decodeString appendFormat:@"%c",(char2Pos&0x0F)<<4];
}
}
}
ret = [NSStringstringWithString:decodeString];
return ret;
}
@end
相关文章推荐
- 【IOS 开发学习总结-OC-9】★ objective-c面向对象之——方法
- 对JSONObject中的数据进行排序
- objective-c 反射
- Object-C 空@selector
- ObjectAnimator详解(测试用,承接Android动画操作中的测试)
- 简要解析XMPP框架及iOS-Objective-C的使用
- 【IOS 开发学习总结-OC-8.2】★★ objective-c面向对象之——类和对象(下)
- NSCFArray insertObject:atIndex:]: mutating method sent to immutable obje的解决方法
- objective-c @protocol
- PyQT中多重继承,其中继承的父类有QObject或QObject的子孙类
- ORA-12545: Connect failed because target host or object does not exist
- JSON JsonArray和JsonObject学习资料
- Android 属性动画(二):ObjectAnimator与ValueAnimator
- objective-c category
- 自动释放池嵌套的简单图解
- Objective-C中的@property的retain写法的原理
- Entity FreamWork 无法创建“System.Object”类型的常量值。此上下文仅支持基元类型或枚举类型错误解决
- 在Win7 x64中使用GNUstep和MinGW编写Objective-C的代码
- 关于Objective-C的内存管理图解
- Object-C学习笔记