且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将日语wstring转换为std :: string

更新时间:2022-10-22 15:34:51

在我看来,它实际上是正确的。



那是UTF-输入的8编码版本(转换前大概是UTF-16),但由于工具链中的某个错误而以ASCII解码形式显示。



您只需要校准文件/终端/显示器以呈现文本输出,就好像它是UTF-8(是)。






还请记住, std :: string 只是字节的容器,并不固有地指定或暗示任何特定的编码。因此,您的问题是如何在Windows中将UTF-16(包含日语字符)转换为UTF-8,或者结果是如何配置终端以显示UTF-8?。



如果此字符串的显示是Visual Studio locals窗口(建议您使用注释出现这种情况,我在本地观察到了 ret字符串的值调试时打开窗口。)您很不走运,因为VS不知道您的字符串采用的编码方式(也不试图找到)。



但对于Visual Studio的其他方面,例如控制台输出窗口,有多种方法可以解决此问题(示例)。


Can anyone suggest a good method to convert a Japanese std::wstring to std::string?

I used the below code. Japanese strings are not converting properly on an English OS.

std::string WstringTostring(std::wstring str)
{
    size_t size = 0;
    _locale_t lc = _create_locale(LC_ALL, "ja.JP.utf8");
    errno_t err = _wcstombs_s_l(&size, NULL, 0, &str[0], _TRUNCATE, lc);
    std::string ret = std::string(size, 0);
    err = _wcstombs_s_l(&size, &ret[0], size, &str[0], _TRUNCATE, lc);
    _free_locale(lc);
    ret.resize(size-1);
    return ret;
}

The wstring is "C\\files\\ブ種別.pdf".

The converted string is "C:\\files\\ブ種別.pdf".

It actually looks right to me.

That is the UTF-8-encoded version of your input (which presumably was UTF-16 before conversion), but shown in its ASCII-decoded form due to a mistake somewhere in your toolchain.

You just need to calibrate your file/terminal/display to render text output as if it were UTF-8 (which it is).


Also, remember that std::string is just a container of bytes, and does not inherently specify or imply any particular encoding. So your question is rather "how can I convert UTF-16 (containing Japanese characters) into UTF-8 in Windows" or, as it turns out, "how do I configure my terminal to display UTF-8?".

If your display for this string is the Visual Studio locals window (which you suggest is the case with your comment "I observed the value of the "ret" string in local window while debugging") you are out of luck, because VS has no idea what encoding your string is in (nor does it attempt to find out).

For other aspects of Visual Studio, though, such as the console output window, there are various approaches to work around this (example).