views:

53

answers:

2

I have an iPhone application using Core Data with an SQLite database in the bottom. I'm writing some text content from the database to a file, but special characters such as Å, Ä and Ö are corrupted in the file (they show up just fine in the application).

When creating and inserting data, I am not using any special encoding. I'm just taking the NSString (entered by the user in a UITextField) and putting it in my persistent objects. When saving the file, I use the following code:

[csvString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

I tried adding a BOM to the beginning of the text ("\xef\xbb\xbf") but it is still corrupted. Anyone has any ideas where the problem might be?

Examples of corrupted characters: å becomes ö, ä becomes ä

A: 

Have you tried changing the encoding to NSUTF16StringEncoding?

Marcus S. Zarra
+1  A: 

Your example corruptions looks like correct UTF-8 characters interpreted as ISO-8859-1. How are you viewing the file? Are you sure that the viewer you are using is actually interpreting the file as UTF-8?

calmh
As always, the problem was no where I thought it was. Turns out that the programs I was reading the data with (OpenOffice Calc and Excel) couldn't manage UTF-8 as simply as I thought. Changing the encoding to UTF16LittleEndian did it.
MW