在iPhone上检测NSString中的Unicode字符 [英] Detect Unicode characters in NSString on iPhone

查看:269
本文介绍了在iPhone上检测NSString中的Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPhone开发一个SMS应用程序。我需要检测用户是否在他们希望发送的NSString中输入了任何unicode字符。



我需要这样做是因为unicode字符在消息中占用更多空间,并且还因为我需要将它们转换为它们的十六进制等价物。 b
$ b

所以我的问题是如何检测NSString中的unicode字符(我从UITextView中读取)的存在。此外,如何将这些字符转换为UCS-2十六进制等价物?



例如:繁= 7E41,体= 4F53,中= 4E2D,文= 6587 <要检查只有ascii字符(或您选择的另一种编码),请使用:
$ b $
b

[myString canBeConvertedToEncoding:NSASCIIStringEncoding];



如果字符串包含非ascii字符,它将返回NO。然后,您可以将字符串转换为UCS-2数据:
$ b [myString dataUsingEncoding:NSUTF16BigEndianStringEncoding];

或NSUTF16LittleEndianStringEncoding取决于您的平台。 UCS-2和UTF-16之间略有不同。 UTF-16取代了UCS-2。你可以阅读这里的差异:



http://en.wikipedia.org/wiki/UTF-16/UCS-2


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

解决方案

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

这篇关于在iPhone上检测NSString中的Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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