views:

1263

answers:

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

+6  A: 

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

Jeremy Bower
Thanks so much. dataUsingEncoding:NSUTF16BigEndianStringEncoding is working great.When I convert this string "文文" using NSUTF16BigEndianStringEncoding and print out (using NSLog) the NSData object I get:<6587 6587> However, what I need to get at is the 65876587 part without the spaces or <> characters. Is there a method in NSData that will give me the raw data.(Apologies, I know I'm most likely using the wrong terms here!)
Fin
NSData is the raw data. It wraps a byte array in an object and provides convenience methods. When you log the NSData, the byte array is formatted with <, > and spaces -- but they're not part of the data. You can get the bytes by using:[myData bytes]You should read the NSData docs. There are a lot of useful methods.
Jeremy Bower