Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape a Unicode character in my Objective-C source code?

I feel incredibly stupid for asking this, but the documentation and Google are giving me no love at all.

I have a Unicode character I want to insert into a string literal in the source code of my iPhone app. I know its hex value. What is the proper escape sequence to use? And for that matter, what obvious source of information am I overlooking that would have told me this?

like image 373
Becca Royal-Gordon Avatar asked May 07 '09 10:05

Becca Royal-Gordon


People also ask

How do you escape unicode?

A unicode escape sequence is a backslash followed by the letter 'u' followed by four hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the four digits. For example, ”\u0041“ matches the target sequence ”A“ when the ASCII character encoding is used.

How does C handle unicode?

It can represent all 1,114,112 Unicode characters. Most C code that deals with strings on a byte-by-byte basis still works, since UTF-8 is fully compatible with 7-bit ASCII. Characters usually require fewer than four bytes. String sort order is preserved.

How do you key a unicode character?

To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

What is a unicode character stored in?

Unicode uses two encoding forms: 8-bit and 16-bit, based on the data type of the data that is being that is being encoded. The default encoding form is 16-bit, where each character is 16 bits (2 bytes) wide. Sixteen-bit encoding form is usually shown as U+hhhh, where hhhh is the hexadecimal code point of the character.


2 Answers

Example:

NSString *stuff = @"The Greek letter Beta looks like this: \u03b2, and the emoji for books looks like this: \U0001F4DA"; 
like image 182
Alex Avatar answered Sep 29 '22 04:09

Alex


If you don't want to put it directly in your string you can use a format specifier like this:

[string stringByAppendingFormat:@"%C", 0x2665]; 
like image 32
Marc Charbonneau Avatar answered Sep 29 '22 03:09

Marc Charbonneau