[英]Conversion from char16_t(utf16) to utf8 doesn't work in visual studio 2015 and 2017
I want to convert the data type char16_t
to utf8, and I have written the following code to do that:我想将数据类型
char16_t
转换为 utf8,为此我编写了以下代码:
// utf16 String
char16_t *wchs = u"UTF16 string";
// A converter object
wstring_convert<codecvt_utf8_utf16<char16_t>, char16_t> conv;
// Convert from utf16 to utf8
string str = conv.to_bytes(wchs);
// print the UTF-16 string after converting to UTF-8
cout << str << endl;
The previous code doesn't work in visual studio whether 2015 or 2017, specifically with "char16_t" data type and gives me an error:无论是 2015 年还是 2017 年,前面的代码在 Visual Studio 中都不起作用,特别是“char16_t”数据类型,并给我一个错误:
error LNK2001: unresolved external symbol "public: static class std::locale::id std::codecvt<char16_t,char,struct _Mbstatet>::id" (?id@?$codecvt@_SDU_Mbstatet@@@std@@2V0locale@2@A)
, error LNK2001: unresolved external symbol "public: static class std::locale::id std::codecvt<char16_t,char,struct _Mbstatet>::id" (?id@?$codecvt@_SDU_Mbstatet@@@std@@2V0locale@2@A)
,
but works well in the GCC compiler.但在 GCC 编译器中运行良好。
Why that happened and how to solve that problem ?为什么会发生这种情况以及如何解决该问题?
You might be out of luck as this seems to be a Visual Studio bug since there are at least 3 posts on the VS Developer Community page which describe the given problem您可能不走运,因为这似乎是一个 Visual Studio 错误,因为 VS 开发人员社区页面上至少有 3 个帖子描述了给定的问题
https://developercommunity.visualstudio.com/content/problem/539181/stdwstring-convert-char16-t-cant-work-in-vs2017.html https://developercommunity.visualstudio.com/content/problem/539181/stdwstring-convert-char16-t-cant-work-in-vs2017.html
https://developercommunity.visualstudio.com/content/problem/65248/unresolved-external-symbol-static-class-stdlocalei.html https://developercommunity.visualstudio.com/content/problem/65248/unresolved-external-symbol-static-class-stdlocalei.html
https://developercommunity.visualstudio.com/content/problem/246272/msconnect-3118643-codecvt-library-issue.html https://developercommunity.visualstudio.com/content/problem/246272/msconnect-3118643-codecvt-library-issue.html
where all of them state that it has been他们都在哪里 state
fixed in: visual studio 2019 version 16.2
固定在:visual studio 2019 版本 16.2
with no indication of a backport to VS 2015 or even VS 2017.没有迹象表明向后移植到 VS 2015 甚至 VS 2017。
Here is a simple function how you can convert from std::u16string to std::string.这是一个简单的 function 如何从 std::u16string 转换为 std::string。 It works with VS 2017.
它适用于 VS 2017。
std::string utf16_to_utf8( std::u16string&& utf16_string )
{
std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> convert;
auto p = reinterpret_cast<const int16_t *>( utf16_string.data() );
return convert.to_bytes( p, p + utf16_string.size() );
}
The reason is the declaration of wstring_convert<codecvt_utf8_utf16<char16_t>, char16_t> conv;
原因是
wstring_convert<codecvt_utf8_utf16<char16_t>, char16_t> conv;
with char16_t template parameter does not compile under VS 2015 and VS2017 due of missing initialization of static locale::id id;
由于缺少
static locale::id id;
in a.cpp file.在 .cpp 文件中。 The int16_t type is working.
int16_t 类型正在工作。
Under Linux with gcc or clang compiler suites it is a little bit easier to convert from std::u16string.在 Linux 和 gcc 或 clang 编译器套件下,从 std::u16string 转换要容易一些。 There is no issue with
static locale::id id;
static locale::id id;
. .
std::string utf16_to_utf8( std::u16string&& utf16_string )
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes( utf16_string );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.