Objective-C版Base64

#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,

    xx0123 45678 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值