macoscocoautf-8nstask

NSTask output special characters convert to UTF8


I'm running lsof through NSTask, pipe output and read into NSData. Then I create NSString with this data:

[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Problem I see, is how NSTask interprets special characters. For file with name: !@#$%^±^&*()ľščťžýáíé.docx I get this result: !@#$%^\xc2\xb1^&*()l\xcc\x8cs\xcc\x8cc\xcc\x8ct\xcc\x8cz\xcc\x8cy\xcc\x81a\xcc\x81i\xcc\x81e\xcc\x81.docx Seems like decomposed UTF8 with hex encoded values. Unfortunately I'm not able to find a way of converting this to proper UTF8.


Solution

  • I found out that setting environment variable LC_ALL to en_US.UTF-8 does the trick.

    [task setEnvironment:@{@"LC_ALL" : @"en_US.UTF-8"}];