简体   繁体   English

Win1251-> UTF16转换

[英]Win1251->UTF16 conversion

I have a dll-project, written in Windows-1251 encoding, and I need my dll's output encoded in UTF-16. 我有一个用Windows-1251编码编写的dll项目,我需要用UTF-16编码的dll输出。 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. 不幸的是,MultiByteToWideChar使用系统区域设置作为源编码。 So for example if my Windows locale is English(USA), it converts Win1252->UTF8, not Win1251->UTF8 as I need. 因此,例如,如果我的Windows语言环境是英语(美国),它会根据需要转换Win1252-> UTF8,而不是Win1251-> UTF8。

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. 据我所知,MultiByteToWideChar总是使用系统区域设置,忽略我的setlocale调用。

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. PS我很抱歉语法错误。

setlocale is a CRT function. setlocale是一个CRT函数。 Obviously Windows API doesn't care what is set via it. 显然,Windows API并不关心通过它设置什么。

You should set the needed codepage (1251 in your case) instead of CP_ACP (which means system default). 您应该设置所需的代码页(在您的情况下为1251)而不是CP_ACP (这意味着系统默认值)。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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