Search code examples
c++localeutf-16multibytewidechar

wctomb chokes on per mille symbol (‰)


I am trying to print out a bunch of unit labels; some of them contain Greek characters, some have other funny code points.

I traced it back to the wctomb functions not knowing what to do with e.g. UTF-16 character 8240:

char mb[10]; 
assert( 0 <= wctomb(mb,8240) );

How can I set the locale used by wctomb to e.g. "All unicode characters"?

How can I find the proper locale name I need, starting from the characters I need?


Solution

  • Setting a correct UTF-8 locale will fix it;

    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <locale.h>
    
    int main()
    {
        setlocale(LC_ALL, "en_US.UTF-8");
        char mb[10]; 
        assert( 0 <= wctomb(mb,8240) );
        printf("%s\n", mb);
        return 0;
    }
    

    See http://ideone.com/sflZj