pythonpython-3.xwinapiunicodewchar

How to convert Unicode string to a TCHAR system?


I have a python (3.6) code that should pass a Unicode string to an Unreal Engine project. The Unreal Engine displays text in TEXT format which I'm not wrong is an array of Win-Api's TCHARs. I saw that on my platform the TCHAR is 2 bytes.

Here is how I encode the string on the python side:

by = bytes(st, 'utf-8')

I tried encoding and passing the string "Hello". The unreal got the data ['H', 'e', 'l', 'l', 'o'] (each char 1 byte), and printed "效汬o" (it treats the "He" and "ll" as a single Unicode character).

How can I fix this?


Solution

  • Given your configuration, TCHAR maps to wchar_t, a character type that is unilaterally encoded using UTF-16LE on Windows.

    You can encode the string using:

    by = bytes(st, 'utf-16')