Just a Computer Graphics Studio & My Life

Archive for 2015 年 10 月 17 日

[iOS] Base64 編碼解碼 (Base64 Encoding Decoding)

先前已經研究果Base64編碼的原理,現在以iOS來實作看看⋯⋯

/**
 Theme: Base64 Encoding Decoding
 IDE: Xcode 7
 Language: Objective C
 Date: 104/10/16
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
    NSString *plainString = @"HappyMan";
    
// Encoding
    NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];
    NSLog(@"%@", base64String); // SGFwcHlNYW4=
    
// Decoding
    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", decodedString); // HappyMan

原來這麼簡單XD~

Base64 chart Base64 Man Base64 HappyMan

參考:Base64 Decoding in iOS 7+

標籤雲

%d 位部落客按了讚: