0

StreamDeck SDK allows to write plugins in C++ and JavaScript. One of the functions I use is setting a key title:

void SetTitle(const std::string &inTitle, const std::string& inContext, ESDSDKTarget inTarget);

inTitle is the title shown on device key. All functions in this project accept only char* strings. Internally, SetTitle generates JSON object and sends it to the StreamDesk program using WebSocket.

I need to set title in different languages. JavaScript plugin allows to do this by simple way, for example:

SetTitle : function(context, keyPressCounter) {
    var json = {
        "event": "setTitle",
        "context": context,
        "payload": {
            //"title": "" + keyPressCounter,
            "title": "ы",
            "target": DestinationEnum.HARDWARE_AND_SOFTWARE
        }
    };

    websocket.send(JSON.stringify(json));
}

This function successfully shows Russian letter "ы" (0x044B) on the device. Using WireShark, I found this packet:

{"event":"setTitle","context":"6AB547E3C555617462ABD483D36ED595","payload":{"title":"\321\213","target":0}}

So, the string is actually 0xd1, 0x8b, or \321\213\. If I use this string in C++ program:

mConnectionManager->SetTitle("\321\213", context, kESDSDKTarget_HardwareAndSoftware);

everything is OK, the letter "ы" is shown. But this is hard-code string. I need to convert any wchar_t* string to char*. What algorithm is used here? How Unicode character 0x044B is converted by third-party program to 0xd1 0x8b? I need to understand this.

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • I would expect that `"\321\213"` is the UTF-8 sequence for Unicode code point `\u044b` (as octal sequences). ...and [UTF-8-Codetabelle mit Unicode-Zeichen](https://www.utf8-zeichentabelle.de/unicode-utf8-table.pl) proved me right. ;-) – Scheff's Cat Jun 03 '20 at 09:36
  • The nice fact about Unicode and UTF-8: The latter is just an encoding of the former. Hence, with a Unicode code point (e.g. a 32 bit number), the UTF-8 can be computed by a simple algorithm. (I'm looking for a suitable answer...) – Scheff's Cat Jun 03 '20 at 09:41
  • 1
    Most answers use conversion functions of `std::` which are IMHO not quite reliable for the future. (Please, note that e.g. [std::codecvt_utf8](https://en.cppreference.com/w/cpp/locale/codecvt_utf8) is marked as deprecated in C++17. I'm not sure about a future replacement.) However, I found this answer to [SO: **UTF8 to/from wide char conversion in STL**](https://stackoverflow.com/a/148766/7478597) which is similar to what we use in our productive code. – Scheff's Cat Jun 03 '20 at 09:47
  • 1
    And a nice general introduction to character encodings is given here: [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/). ;-) – Scheff's Cat Jun 03 '20 at 09:53
  • @Scheff: got it working, thanks. – Alex F Jun 03 '20 at 11:26

0 Answers0