I'm having issues working with iOS Swift 2.0 to perform an XOR on a [UInt8] and convert the XORd result to a String. I'm having to interface with a crude server that wants to do simple XOR encryption with a predefined array of UInt8 values and return that result as a String.
Using iOS Swift 2.0 Playground, create the following array:
let xorResult : [UInt8] = [24, 48, 160, 212] // XORd result
let result = NSString(bytes: xorResult, length: xorResult.count, encoding: NSUTF8StringEncoding)
To store an arbitrary chunk of binary data as as a string, you need
a string encoding which maps each single byte (
0 ... 255) to some
UTF-8 does not have this property, as for example
is the start of a multi-byte UTF-8 sequence and not valid on its own.
The simplest encoding with this property is the ISO Latin 1 aka
encoding. According to 8859-1.TXT,
the Unicode characters
U+0000 .. U+00FF are mapped exactly
to the bytes
0x00 .. 0xFF. This encoding is available for