NSTask字符串编码问题 [英] NSTask string encoding problem
问题描述
问题是我可能会像RíoGallegos这样的词条。可悲的是(我用fileSystemRepresentation检查),经过转换,结果是RiÃÅoGallegos。
我该如何解决?
- Ry
问题是我可能grep条款如RíoGallegos。可悲的是(我用fileSystemRepresentation检查),经过转换,结果是RiÃÅoGallegos。
这是一个可能的解释。你的意思是RíoGallegos被转换为Ricode> \xcc\x81 o Gallegos - UTF-8字节来表示分解的i +
您的问题是,grep不会将这些字节解释为UTF-8。 grep正在使用一些其他编码,显然是MacRoman。
解决方案是告诉grep使用UTF-8。这需要在您的grep任务的 LC_ALL 变量Classes / NSTask_Class / Reference / Reference.html#// apple_ref / occ / instm / NSTask / setEnvironment:rel =nofollow noreferrer>环境。
使用的快速而肮脏的值将是en_US.UTF-8;更正确的方法是获取用户的主要首选语言,用下划线替换连字符(如果有的话),并在结尾贴上.UTF-8
In my program, I'm grep-ing via NSTask. For some reason, sometimes I would get no results (even though the code was apparently the same as the command run from the CLI which worked just fine), so I checked through my code and found, in Apple's documentation, that when adding arguments to an NSTask object, "the NSTask object converts both path and the strings in arguments to appropriate C-style strings (using fileSystemRepresentation) before passing them to the task via argv[]" (snip).
The problem is that I might grep terms like "Río Gallegos". Sadly (as I checked with fileSystemRepresentation), that undergoes the conversion and turns out to be "RiÃÅo Gallegos".
How can I solve this?
-- Ry
The problem is that I might grep terms like "Río Gallegos". Sadly (as I checked with fileSystemRepresentation), that undergoes the conversion and turns out to be "RiÃÅo Gallegos".
That's one possible interpretation. What you mean is that "Río Gallegos" gets converted to "Ri\xcc\x81
o Gallegos"—the UTF-8 bytes to represent the decomposed i + combining acute accent.
Your problem is that grep is not interpreting these bytes as UTF-8. grep is using some other encoding—apparently, MacRoman.
The solution is to tell grep to use UTF-8. That requires setting the LC_ALL
variable in your grep task's environment.
The quick and dirty value to use would be "en_US.UTF-8"; a more proper way would be to get the language code for the user's primary preferred language, replace the hyphen, if any, with an underscore, and stick ".UTF-8" on the end of that.
这篇关于NSTask字符串编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!