I am making a simple CSV file in my application, and I use characters such as 'æøå'. When I open the file on my Mac with BBEdit it opens the file correctly, but if I import the file in Excel (MS Office 2011) with Unicode UTF-8 it does not show the characters correctly. How do I fix this?
Some of the code:
NSString *tmpPath = NSTemporaryDirectory();
NSString *fullPath = [tmpPath stringByAppendingPathComponent:#"log_export.csv"];
[csvComplete writeToFile:fullPath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
I am mailing the csv.
[mailController addAttachmentData:data mimeType:#"text/csv" fileName:#"sertifikat-loggbok.csv"];
CSV is a text format that does not specify the character encoding. Different applications assume different encodings.
My experience is that Microsoft Excel assumes it's in the default encoding which happens to be CP1250 (similar to ISO 8859-1 / Latin 1 encoding) on my machine. Maybe it always assumes CP1250.
So try to switch from UTF-8 to CP1250. But I cannot guarantee it'll always work because there simply is no encoding specified for CSV.
Related
For some of you (I'm sure) this question is quite simple to answer, but I have some difficulties in understanding how to solve the problem.
I have a .txt file containing a table like this:
" 236
? 26
x00EE 16
As you probably understood the left column lists symbols and the right one lists some code of the, I defined in my app.
And... you probably understood that, within symbols, there are some "strange". The 0x00EE should be the "å" (a with a ring above).
Unfortunately I can't control the left column, i.e. it comes from another software. Making some experiments I found that:
NSLog( #"\x00ee" );
for example produces a waring telling the hte code does not belong to the UTF-8 range.
So I was wandering how to convert the NSString #"\x00ee" (that I read from file, so is a string composed of 6 chars) to the unique unicode letter "å" (a with a ring above).
Can anyone help me?
Thanks...
You need to find out what character set encoding was used. 0xEE is unicode for î. In Unicode, å is E5. This is encoded in UTF-8 as the sequence 0xC3 0xA5. The following does the trick for me:
NSLog(#"\xc3\xa5");
If your input string contains only ASCII characters then you can use the fact that
NSNonLossyASCIIStringEncoding decodes \uNNNN to the corresponding Unicode character:
NSString *s = #"\\x00ee"; // from your text file
NSString *s1 = [s stringByReplacingOccurrencesOfString:#"\\x" withString:#"\\u"];
NSData *d = [s1 dataUsingEncoding:NSASCIIStringEncoding];
NSString *s2 = [[NSString alloc] initWithData:d encoding:NSNonLossyASCIIStringEncoding];
NSLog (#"%#", s2);
Output: î, which is U+00EE (LATIN SMALL LETTER I WITH CIRCUMFLEX).
(Remark: å is U+00E5, not U+00EE).
I want to encode a short title in filenames. The problem is that occasionally the title will contain a character such as a colon or a slash. Is there a standard encoding that would be typical/appropriate for this?
EDIT: to clarify, I want to encode the title in such a way that the encoded title could be used as a filename. Or is that called percent escaping?
The way I do this is with a category on NSURL, which I use to get the NSURL for a filename in a particular directory. Once I have this NSURL, I can fetch or save the file using the URL after performing the usual checks about whether or not the file already exists and handling those cases accordingly.
The relevant code snippet is:
+ (NSURL *)adnURLForFileName:(NSString *)fileName inDirectory:(NSSearchPathDirectory)searchDirectory {
NSString *percentEscapedFileName = [fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *URLForDirectory = [[fileManager URLsForDirectory:searchDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
return [NSURL URLWithString:percentEscapedFileName relativeToURL:URLForDirectory];
}
You can download the full category code from GitHub - NSURL+ADNFileHelpers
You could use -stringByReplacingOccurrencesOfString:withString: to replace the slash character with U+2044, the "solidus" aka "fraction slash". It looks like this: ⁄
http://en.wikipedia.org/wiki/Solidus_(punctuation)
The slash is not allowed in the Unix APIs. The colon is not allowed in HFS and in the old File Manager APIs. The same filename character will show up as a colon in the former and as a slash in the latter. In practice: you can use the Finder to rename a file to "/" (because the Finder uses the traditional Mac separator of :), but it will show up as ":" if you use ls.
If you need to allow both colons and slashes, you need to encode the characters somehow. You could use URL-style escaping, but if you expect the user to look at the filename in the Finder or in some other program, it's going to look horrible. It's better to escape just the path separator. For example, if you're using the Unix style APIs (path separator /), you could encode / as :- and : as :: (to avoid ambiguity). Or you could use some other little-used character for the escape.
I have approached this problem by filtering the title before using it in the filename. NSString has some useful methods, such as stringByStandardizingPath and stringByReplacingOccurrencesOfString:withString:. The filtering approach is lossy, in that the original title information might not be restorable. Similarly, I don't think encoding would work because iOS allows such a wide range of characters in its filenames. One possible alternative solution could be a plist archive with key=filename, value=title.
I have a list of several locations, some of them containing the letters æ, Æ, ø, Ø, å and Å.
From the webservice I'm using, the letters comes out as "ø ;" "Å ;" etc.
When I download the feed from the webservice, I use UTF-8 encoding.
How can I decode the occurences of these characters?
Thanks!
There is no standard way, to make it simple write your own custom method (or NSString extension) and do this :
string = [string stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
If your webservice is using utf8 and if you decode the data with [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], all should be ok.
A NSString category called "GTMNSString+HTML" written by Google works perfectly for me. Check it out here: https://gist.github.com/takuma104/ntlniph/blob/master/gtm/Foundation/GTMNSString+HTML.h & here: https://gist.github.com/takuma104/ntlniph/blob/master/gtm/Foundation/GTMNSString+HTML.m
I've been trying to solve the encoding conversion problem without any luck so far. I found many suggestions on Stack Overflow how to tackle the problem like converting the XML string into NSData that uses UTF8 encoding, but the result was the same, my Spanish tildas are presented as weird chars. This is what I am using to grab the xml:
//Convert Win 1252 encoding of the string to UTF8
NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:chosenDrawRss] encoding:NSWindowsCP1252StringEncoding error:&error];
If I call the above method with encoding UTF8 app crushes...
I tried converting the string into NSData using UTF8 and then back to NSString but still I had no luck. I wish I could simply change the XML file encoding but unfortunately that is out of my hands :(
When printed to NSLog everything is presented as it should be whether Im printing the XML string or the NSData created from that string...
BEst regards,
L
UPDATE: I haven't found the solution for the encoding mess :(. Since I found out that my Win1252 encoded RSS was also containing some HTML that I wanted to get rid of I wrote a .php script that I am calling from iPhone. This .php script parses the original XML from the remote server. This .php script is in UTF8; does some html cleaning and reorders the XML elements so in my case it made sense doing it is this way. Unfortunately, I still have no clue how to read a win1252 encoded XML and convert it to UTF8 directly from iOS :(((
I'm using NSFileManager to create a file at a specified path with text view contents.
I'm using following code
[[NSFileManager defaultManager] createFileAtPath:saveFileName contents:[[tView text] dataUsingEncoding:NSASCIIStringEncoding] attributes:nil];
But the problem is that, its creating file with no contents. I'm sure that text contents are not nil.
How can I check and confirm that data is written into file.
Some suggestions:
Capture the BOOL return of the line to make sure it is completing.
Log the value of [tView text] to make sure it is not empty.
ASCII encoding can cause problems these days. Try NSUTF8StringEncoding or NSUTF16StringEncoding.
Localization may be a problem with plain ASCII encoding because ASCII does not handle multibyte character encodings.
Well, I know it's few months late, but you're sending NSString instead of NSData to the contents: parameter.
Try:
[[tView text] dataUsingEncoding:NSUTF8StringEncoding]