简体   繁体   中英

Win1251->UTF16 conversion

I have a dll-project, written in Windows-1251 encoding, and I need my dll's output encoded in UTF-16. I use the following function to do conversion:

ptr = MultiByteToWideChar(CP_ACP, 0, str, -1, wbuff.getBuffer(), len);

Unfortunately, MultiByteToWideChar uses system locale as a source encoding. So for example if my Windows locale is English(USA), it converts Win1252->UTF8, not Win1251->UTF8 as I need.

I tried to set locale manually, but the following code doesn't work either:

enc = setlocale(CL_ALL, "rus_rus.1251");
//this returns Windows-1251 encoding
ptr = MultiByteToWideChar(CP_ACP, 0, str, -1, wbuff.getBuffer(), len);

As I understand, MultiByteToWideChar always uses a system locale, ignoring my setlocale call.

Is there any other ways to do such conversion? Or may be I just don't understand these locale settings properly? Thanks.

PS I'm sorry for grammar mistakes.

setlocale is a CRT function. Obviously Windows API doesn't care what is set via it.

You should set the needed codepage (1251 in your case) instead of CP_ACP (which means system default).

res = MultiByteToWideChar(1251, 0, str, -1, wbuff.getBuffer(), len);

如果您不被迫使用WinAPI,则可能需要使用ICU的字符转换器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM