NSTask字符串编码问题 [英] NSTask string encoding problem

查看:211
本文介绍了NSTask字符串编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我通过NSTask进行grep。由于某种原因,有时我会得到结果(即使代码与从命令行运行的命令显然相同),所以我检查了我的代码,并在Apple的文档中发现,当添加参数时NSTask对象,NSTask对象将参数中的路径和字符串都转换为适当的C风格字符串(使用fileSystemRepresentation),然后通过argv []将其传递到任务之前( snip



问题是我可能会像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\x81o 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆