To keep this simple, I'll only be encoding/decoding a single byte.
If I encode the byte 127, I get the base64 string "fw==" which can be successfully decoded back to byte 127. If, however, I encode a byte ≥ 128, then even though I can produce a base64 string without error (for example, byte 128 gives the string "gA=="), I get an error when I try to decode it.
Here's my code which can be copy-pasted into any Xcode playground to reproduce the problem:
func stringToByteArray(string: String) -> [UInt8] {
var bytes: [UInt8] = [];
for code in string.utf8 {
bytes.append(UInt8(code));
}
return bytes;
}
func byteArrayToBase64(bytes: [UInt8]) -> String {
let nsdata: NSData = NSData(bytes: bytes as [Byte], length: bytes.count)
let base64Encoded: NSString = nsdata.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0));
return String(base64Encoded);
}
func base64ToByteArray(base64String: String) -> [UInt8] {
let nsdata: NSData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))!
let base64Decoded: NSString = NSString(data: nsdata, encoding: NSUTF8StringEncoding)!
return stringToByteArray(String(base64Decoded));
}
/* Replacing 127 with 128 below or greater produces an error */
var testString = byteArrayToBase64([127]);
base64ToByteArray(testString)
The problem is here:
You convert the decoded data to a string. This fails for
[128]
because that does not represent a valid UTF-8 sequence.Here is a version that avoids the intermediate string:
Remarks:
options: NSDataBase64DecodingOptions(rawValue: 0)
can be simplified tooptions: nil
.baseString
is not a valid Base64 string. You could change it to return an optional.Then it would look like this:
Example usage:
Update for Swift 2 / Xcode 7: