If you really need to convert your data to a string, use a Unicode encoding, as unicode provide a codepoint for every possible character. The most common unicode encoding is obviously UTF-8 (NSUTF8StringEncoding
)
But if this is arbitrary data that you need to transfer, and that data does not represent a string, don't convert it into a string, whatever the encoding. It will leads to problems (e.g if your data contains a zero byte, it will be considered as a string terminator, etc.)
Instead, if you need to transfer some raw data to your server, consider:
- Using POST instead of GET if possible
- Encode your data in Base64, so that you ensure you won't loose any data when converting nor during your transport from your client to your server (or worse, corrupt your data by converting to an arbitrary encoding)
- If possible on your server (if it is yours and you have access to the code), decode your Base64-encoded string back to raw data upon reception, and store it in a BLOB field instead of in a VARCHAR or BIGTEXT field.
- If you can't access the server code, and have no choice of storing it in a TEXT/VARCHAR field in your db, you can still store the Base64-encoded string representing your data directly in your database (and decode it when you retrieve it from the DB later).
See Google for Base64 encoding in Objective-C, there are plenty of references to a widely used NSString+Base64 category.