1

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?

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Use `%C` instead of `%c`. `%C` supports a bigger range. – rmaddy Feb 19 '14 at 18:09
  • Your question is inconsistent. If you want the characters without a "lookup table", where do you expect to find your numbers? I mean you can find the codes in [online tables](http://www.fileformat.info/info/charset/UTF-16/list.htm), but why look them up if you can simply type them into a NSString literal? – Hot Licks Feb 19 '14 at 18:31
  • Don't forget part 2: Is there an online utility to lookup the "magic" decimal constants required for %C ? Or can the lookup be done locally in a web browser using a few lines of javascript? – hotpaw2 Feb 19 '14 at 18:31
  • @HotLicks: I'm a passing a fixed length numeric value over a hardware link. 8-bit CPU doesn't know NSString literals. – hotpaw2 Feb 19 '14 at 18:33
  • Then pass the character code. If you're limited to European languages you can probably get along fine with UTF16 -- 16 bits to the character. – Hot Licks Feb 19 '14 at 20:16

2 Answers2

2

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Or you can just include the character between quotes, in a NSString literal. – Hot Licks Feb 19 '14 at 18:10
  • @HotLicks The OP states they have a numeric value that needs to be converted. – rmaddy Feb 19 '14 at 18:10
  • They also said "How does one create an NSString for non-ASCII characters such as:..." – Hot Licks Feb 19 '14 at 18:11
  • @hotpaw2: In Xcode, goto "Edit -> Special Characters ...". A character viewer pops up. Choose "Customize List ..." from the top-left pop-up menu. Add "Code Tables -> Unicode". Now select "Unicode" on the left side of the character viewer. (Compare Nitin Gohel's answer to http://stackoverflow.com/questions/17781457/how-do-i-do-that-insert-image-in-code.) – Martin R Feb 19 '14 at 18:31
2

You can use

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

with uppercase %C for unicode characters. See here!

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • 1
    While I suggested this in a comment, it is important to note that this only works with characters with a Unicode value up to `\UFFFF`. Martin's solution can also handle higher characters. – rmaddy Feb 19 '14 at 18:13
  • @rmaddy I'm sorry, I didn't notice your comment until I finished mine. – Merlevede Feb 19 '14 at 18:17
  • I'm pretty sure, that Martin's uint32_t cannot handle unicode values higher than 0xffff. ;-) – Amin Negm-Awad Feb 19 '14 at 19:22
  • @AminNegm-Awad: That is interesting, what makes you think that? `0x01F604` is greater than `0xFFFF` and I have tested my code before posting it. (Btw., the largest possible `uint32_t` value is 0xFFFFFFFF`). – Martin R Feb 19 '14 at 20:01
  • 1
    *Boing* Miscounted: 16 instead of 32. – Amin Negm-Awad Feb 19 '14 at 20:02