1
votes

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.

3

3 Answers

3
votes

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"}];
0
votes

It work for me.

    NSTask *task = [[NSTask alloc] init];
    NSMutableDictionary * e = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]];
    [e setObject:@"en_US.UTF-8" forKey:@"LC_ALL"];
    [e setObject:@"en_US.UTF-8" forKey:@"LANG"];
    [task setEnvironment:e];
0
votes

In Swift NSTask is replaced by Process:

let process: Process = Process()
process.environment = ["LC_ALL": "en_US.UTF-8"]