美文网首页
iOS Base64

iOS Base64

作者: 爃少 | 来源:发表于2016-07-04 11:05 被阅读110次
    #import <Foundation/Foundation.h>
    @interface NSString (Base64)/** * 转换为Base64编码 */ 
    - (NSString *)base64EncodedString; /** * 将Base64编码还原 */ 
    - (NSString *)base64DecodedString; 
    @end
    
    @implementation NSString (JCBase64)
    - (NSString *)base64EncodedString;
    {
        NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
        return [data base64EncodedStringWithOptions:0];
    }
    
    - (NSString *)base64DecodedString
    {
        NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
        return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    }
    @end
    

    调用:

    NSString *codeString = @"Hello world";
    NSLog(@"原文--%@",codeString);NSString *base64Str = [codeString base64EncodedString];
    NSLog(@"Base64编码--%@",base64Str);NSString *decodeStr = [base64Str base64DecodedString];
    NSLog(@"Base64解码--%@",decodeStr);
    

    相关文章

      网友评论

          本文标题:iOS Base64

          本文链接:https://www.haomeiwen.com/subject/hrbcjttx.html