How to convert Unicode string to a TCHAR system?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 02:55:46

问题


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?

  • Should I change the encoding on the python side to always generate 2 bytes per char?
  • Should I decode the result byte array on unreal to TCHAR Unicode somehow?

回答1:


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')


来源:https://stackoverflow.com/questions/52948522/how-to-convert-unicode-string-to-a-tchar-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!