January 07, 2006

printf and Unicode

In the "you learn something new every day" category, I just learned that you can insert Unicode characters into printf-style format strings by using %C. I was trying to insert an emdash — Unicode character 0x2014 — into an NSString and couldn't find a good way to do it until I found some other code that did it with %C.

It's documented, too — the man page for printf(3) says this for 'C' and 'c':


C
Treated as c with the l (ell) modifier.
...
c
...
If the l (ell) modifier is used, the wint_t argument shall be converted to a wchar_t, and the (potentially multi-byte) sequence representing the single wide character is written, including any shift sequences. If a shift sequence is used, the shift state is also restored to the original state after the character.