Search code examples
c++arrayswxwidgetscharacter-set

How to store Unicode characters in an array?


I'm writing a C++ wxWidgets calculator application, and I need to store the characters for the operators in an array. I have something like int ops[10] = {'+', '-', '*', '/', '^'};. What if I wanted to also store characters such as , ÷ and × in said array, in a way so that they are also displayable inside a wxTextCtrl and a custom button?


Solution

  • This is actually a hairy question, even though it does not look like it at first. Your best option is to use Unicode Control Sequences instead of adding the special characters with your Source Code Editor.

    wxString ops[]={L"+", L"-", L"*", L"\u00F7"};

    You need to make sure that characters such as √, ÷ and × are being compiled correctly.

    Your sourcefile (.cpp) needs to store them in a way that ensures the compiler is generating the correct characters. This is harder than it looks, especially when svn, git, windows and linux are involved.

    Most of the time .cpp files are ANSI or 8 bit encoded and do not support Unicode constants out of the box.

    You could save your sourcefile in UTF-8 codepage so that these characters are preserved. But not all compilers accept UTF-8

    The best way to do that is to encode these using Unicode control characters. wxString div(L"\u00F7"); is the string for ÷. Or in your case perhaps wxChar div('\u00F7'). You have to look up the Unicode control sequences for the other special chars. This way your source file will contain ANSI chars only and will be accepted by all compilers. You will also avoid code page problems when you exchange source files with different OS platforms.

    Then you have to make sure that you compile wxWidgets with UNICODE awareness (although I think this is the default for wx3.x). Then, if your OS supports it, these special characters should show up.

    Read up on Unicode controls (Wikipedia). Also good input is found in utf8everywhere.org. The file .editorconfig can also be of help.