Saturday, March 12, 2016

Convert NSData/[UInt8] to Base64 in Swift

Update - May 19, 2017 with Swift 3.1 (Xcode 8.3.2)

The code below shows how to convert String/NSData/[UInt8] to base64 data. The code involves two parts:

1. Conversion of NSData to base64 and then decoding the base64 data:

String -> NSData -> base64 NSData -> NSData -> NSString -> String

2. Conversion of [UInt8] to base64 and then decoding the base64 data:

String -> [UInt8] -> NSData -> base64 NSData -> NSData -> NSString



Swift code:

Update - May 19, 2017 Swift 3.1 (Xcode 8.3.2)

let myString = "This is my string."

//1. Convert String to base64
//Convert string to NSData
let myNSData = myString.data(using: String.Encoding.utf8)! as NSData

//Encode to base64
let myBase64Data = myNSData.base64EncodedData(options: NSData.Base64EncodingOptions.endLineWithLineFeed)

//Decode base64
let resultData = NSData(base64Encoded: myBase64Data, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!

//Convert NSData to NSString
let resultNSString = NSString(data: resultData as Data, encoding: String.Encoding.utf8.rawValue)!

//Convert NSString to String
let resultString = resultNSString as String

print(resultString)

//2. Convert [UInt8] to base64
//Convert string to [UInt8]
let array : [UInt8] = Array(myString.utf8)

//Convert [UInt8] to NSData
let data = NSData(bytes: array, length: array.count)

//Encode to base64
let base64Data = data.base64EncodedData(options: NSData.Base64EncodingOptions.endLineWithLineFeed)

//Decode base64
let newData = NSData(base64Encoded: base64Data, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!

//Convert NSData to NSString
let newNSString = NSString(data: newData as Data, encoding: String.Encoding.utf8.rawValue)!

print(newNSString)

Original Post: March 12, 2016 with Swift 2.1

//1. Convert String to base64
//Convert string to NSData
let myData = myString.dataUsingEncoding(NSUTF8StringEncoding)!
        
//Encode to base64
let myBase64Data = myData.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        
//Decode base64
let resultData = NSData(base64EncodedData: myBase64Data, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
        
//Convert NSData to NSString
let resultNSString = NSString(data: resultData, encoding: NSUTF8StringEncoding)!

//Convert NSString to String
let resultString = resultNSString as String
        
print(resultString)
        
//2. Convert [UInt8] to base64
//Convert string to [UInt8]
let array : [UInt8] = Array(myString.utf8)
        
//Convert [UInt8] to NSData
let data = NSData(bytes: array, length: array.count)
        
//Encode to base64
let base64Data = data.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        
//Decode base64
let newData = NSData(base64EncodedData: base64Data, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
        
//Convert NSData to NSString
let newNSString = NSString(data: newData, encoding: NSUTF8StringEncoding)!
        
print(newNSString)


More Information:

Conversion between String, NSString, NSData and [UInt8] array in Swift

1 comment: