Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Unicode characters in NSString on iPhone

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

like image 832
3 revs Avatar asked Nov 10 '09 09:11

3 revs


People also ask

How do you read Unicode?

Unicode is really just another type of character encoding, it's still a lookup of bits -> characters. The main difference between Unicode and ASCII is that Unicode allows characters to be up to 32 bits wide. That's over 4 billion unique values.

Does Unicode work on all devices?

The Unicode Standard provides a unique number for every character, no matter what platform, device, application or language. It has been adopted by all modern software providers and now allows data to be transported through many different platforms, devices and applications without corruption.

How do I type Unicode in a text message?

People wanting to use Unicode characters in an SMS text message sent from a mobile device should find the Unicode character set included in their devices´ settings (Menu > Messages > Settings > SMS > Sending Preferences > Alphabet).


1 Answers

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

like image 124
Jeremy Bower Avatar answered Oct 31 '22 01:10

Jeremy Bower