首页 > 其他分享 >iOS base64 编码详解

iOS base64 编码详解

时间:2023-05-22 19:07:08浏览次数:59  
标签:NSInteger NSLog base64 iOS NSString value length 详解 data


iOS base64 编码详解

iOS中将NSData转为base64编码时有NSDataBase64EncodingOptions

NSDataBase64EncodingOptions有四个选项

/****************        Base 64 Options    ****************/

typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
    // Use zero or one of the following to control the maximum line length after which a line ending is inserted. No line endings are inserted by default.
    NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
    NSDataBase64Encoding76CharacterLineLength = 1UL << 1,

    // Use zero or more of the following to specify which kind of line ending is inserted. The default line ending is CR LF.
    NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
    NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,

} NS_ENUM_AVAILABLE(10_9, 7_0);

这四个选项是内部实现算法不同但是结果是一致的。
NSString *testBase64 = @"123456";
    NSData *data = [testBase64 dataUsingEncoding:NSUTF8StringEncoding];

    NSString *base64Str1 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    NSString *base64Str2 = [self encode:data.bytes length:data.length];

    NSString *base64Str3 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
    NSString *base64Str4 = [data base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
    NSString *base64Str5 = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    NSLog(@"the base64Str1 is %@\n", base64Str1);
    NSLog(@"the base64Str2 is %@\n", base64Str2);

    NSLog(@"the base64Str3 is %@\n", base64Str3);
    NSLog(@"the base64Str4 is %@\n", base64Str4);
    NSLog(@"the base64Str5 is %@\n", base64Str5);
//给出一个base64的c实现方式
- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData * data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t * output = (uint8_t *)data.mutableBytes;

    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    table[(value >> 18) & 0x3F];
        output[index + 1] =                    table[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}


标签:NSInteger,NSLog,base64,iOS,NSString,value,length,详解,data
From: https://blog.51cto.com/u_16124099/6326563

相关文章

  • 信号量和互斥锁详解 非MarkDown版
    [参考链接1](http://blog.chinaunix.net/uid-24612247-id-2305050.html)详细说明:```信号量强调的是线程(或进程)间的同步:“信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候......
  • iOS 内购详解
    iOSApp内购SKProductsRequestDelegate不调用原因及解决办法之前代码是这样写的://此时request没有强指针指向它这样会导致SKProductsRequestDelegate不调用但不是每次都不调用,只是偶尔出现不调用的情况这个问题不好发现SKProductsRequest*request=[[SKProductsReq......
  • 信号量和互斥锁详解 MarkDown版
    信号量和互斥锁详解参考链接1参考链接2参考链接3参考链接4详细说明:信号量强调的是线程(或进程)间的同步:“信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候,就阻塞在那里)。当信号量为单值信号量是,也......
  • 线程条件变量pthread_cond_t和线程条件锁详解
    线程条件变量pthread_cond_t和线程条件锁详解参考文章条件变量常与互斥锁同时使用,达到线程同步的目的:条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足。APUE上,关于条件锁。其中有2条总结:1.使用条件锁前必须先锁住对应的互斥锁。2.条件锁进入阻塞(pthrea......
  • iOS ijkplayer 播放器 消息循环 详解
    iOSijkplayer 消息循环_mediaPlayer=ijkmp_ios_create(media_player_msg_loop);//开始消息循环intmedia_player_msg_loop(void*arg){@autoreleasepool{IjkMediaPlayer*mp=(IjkMediaPlayer*)arg;__weakIJKFFMoviePlayerController*ffpControll......
  • iOS UITabBarController 典型应用
    -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlau......
  • iOS Apple Development Document 详解
    iOS官方开发文档https://developer.apple.com/library/prerelease/ios/navigation/点击打开链接再次标记一下。......
  • 基于alios(centos 7.2) 安装 docker
    参考文档:https://help.aliyun.com/document_detail/51853.html?spm=a2c4g.264695.0.0.55c91e18zfwqrz#section-gtl-cjs-ls2https://developer.aliyun.com/article/1148896https://blog.csdn.net/qq_32828933/article/details/88656414https://github.com/huataihuang/cloud-a......
  • iOS Label 作为分割线时 上下左右切边
    -(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.//CGFloatborderWidth=20.0;//UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(100,400,100,100)];......
  • iOS 免费后台系统 Bmob平台
    Bmob平台为您的移动应用提供了一个完整的后端解决方案,我们提供轻量级的SDK开发包,让开发者以最小的配置和最简单的方式使用Bmob平台提供的服务,进而完全消除开发者编写服务器代码以及维护服务器的操作。使用CocoaPods安装BmobSDK如何使用CocoaPods安装BmobSDK可查看我们提供的文档:......