将日语wstring转换为std :: string [英] Convert Japanese wstring to std::string

查看:150
本文介绍了将日语wstring转换为std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能建议一种将日语 std :: wstring 转换为 std :: string 的好方法吗? / p>

我使用了以下代码。日语字符串在英语操作系统上无法正确转换。

  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_1(& size,& ret [0],size,& str [0],_TRUNCATE,lc);
_free_locale(lc);
ret.resize(size-1);
回程;
}

wstring C\\files\\ブ种别.pdf



转换后的 string C:\\files\\ブ種別.pdf

解决方案

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



那是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).

这篇关于将日语wstring转换为std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆