在 iPhone 上检测 NSString 中的 Unicode 字符
我正在开发 iPhone 的短信应用程序。我需要检测用户是否在他们希望发送的 NSString 中输入了任何 unicode 字符。
我需要这样做是因为 unicode 字符在消息中占用更多空间,而且还因为我需要将它们转换为相应的十六进制字符。
所以我的问题是如何检测 NSString (我从 UITextView 读取的)中是否存在 unicode 字符。另外,如何将这些字符转换为其 UCS-2 十六进制等效值?
例如繁=7E41,体=4F53,中=4E2D,文=6587
I am working on an SMS application for the iPhone. I need to detect if the user has entered any unicode characters inside the NSString they wish to send.
I need to do this is because unicode characters take up more space in the message, and also because I need to convert them into their hexadecimal equivalents.
So my question is how do I detect the presence of a unicode character in an NSString (which I read from a UITextView). Also, how do I then convert those characters into their UCS‐2 hexadecimal equivalents?
E.g 繁 = 7E41, 体 = 4F53, 中 = 4E2D, 文 = 6587
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要仅检查 ascii 字符(或您选择的其他编码),请使用:
[myString canBeConvertedToEncoding:NSASCIIStringEncoding];
如果字符串包含非 ASCII 字符,它将返回 NO。然后,您可以使用以下命令将字符串转换为 UCS-2 数据:
[myString dataUsingEncoding:NSUTF16BigEndianStringEncoding];
或 NSUTF16LittleEndianStringEncoding,具体取决于您的平台。 UCS-2 和 UTF-16 之间存在细微差别。 UTF-16 已取代 UCS-2。您可以在此处了解差异:
http://en.wikipedia.org/wiki /UTF-16/UCS-2
To check for only ascii characters (or another encoding of your choice) use:
[myString canBeConvertedToEncoding:NSASCIIStringEncoding];
It will return NO if the string contains non-ascii characters. You can then convert the string to UCS-2 data with:
[myString dataUsingEncoding:NSUTF16BigEndianStringEncoding];
or NSUTF16LittleEndianStringEncoding depending on your platform. There are slight differences between UCS-2 and UTF-16. UTF-16 has superseded UCS-2. You can read about the differences here:
http://en.wikipedia.org/wiki/UTF-16/UCS-2
我无法让它发挥作用。
我有一个 html 字符串,其中包含
NON BREAKING SPACE
我尝试了 3 种类型的编码/解码,
但这些都不起作用。
它们似乎工作得好像我 NSLog 字符串它看起来不错
哪个输出
但我碰巧将这些存储在 NSArray 中并且我碰巧调用
[stringArray description]
并且所有 unicode 仍然在那里所以有些东西在 NSLog 中隐藏
但它显示在 NSArray 描述中,因此您可能认为您已经删除了 Unicode,但实际上并没有。
将尝试另一种方法来替换字符。
I couldn't get this to work.
I has a html string with
NON BREAKING SPACE
I tried 3 types of encode/decode
none of these worked.
They seemed to work as if I NSLog the string it looks ok
Which output
but I happened to store these in an NSArray and I happened to call
[stringArray description]
and all the unicode was still in thereSo something in NSLog hides
but it shows up in NSArray description so you may think youve removed the Unicode when you haven't.
Will try another method that replace the characters.