Question

To convert a numeric decimal value x in the range of 97 to 122 into an NSString representing that ASCII character, on can simply call:

[ NSString stringWithFormat: @"%c", x ]

How does one create an NSString for non-ASCII characters such as:

ä , è , ñ , à , é , ç , ĝ , ŝ , š , ĥ , þ , ð , ĵ , ü , ś , ź , ż

from a strictly numeric input value? (e.g. decimal UInt32, and not copied from or requiring any other NSString, Objective-C, or Core Foundation data type in a lookup table).

Is there some online tool that will look up whatever magic decimal numeric values (UTF-foo?) are require for each character?

Was it helpful?

Solution

If you have the Unicode value then you can do

uint32_t code = 0x0125; // U+0125 is 'ĥ'
NSString *s = [[NSString alloc] initWithBytes:&code length:4 
                            encoding:NSUTF32LittleEndianStringEncoding];

This works even for Unicodes outside the "Basic Multilingual Plane", such as

uint32_t code = 0x01F604; // 😄 = SMILING FACE WITH OPEN MOUTH AND SMILING EYES

The above code assumes that integers are stored in little-endian byte order (which is the case for all current processors running iOS or OS X). A byte-order independent method is

uint32_t code = OSSwapHostToLittleInt32(0x0125);

In Xcode, you can lookup the Unicodes in the "Character Viewer" from the "Edit -> Special Characters ..." Menu. Of course there are also tables for all Unicodes at Unicode 6.3 Character Code Charts.

OTHER TIPS

You can use

[NSString stringWithFormat:@"%C", ch ];

with uppercase %C for unicode characters. See here!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top