[英]Handling UTF-8 encoded strings between std::wstring and std::string
I am using two libraries one that stores UTF-8 strings in std::wstring
and another stores strings ( UTF-8) in std::string
. 我使用两个库,一个在
std::wstring
中存储UTF-8字符串,另一个在std::string
存储字符串(UTF-8)。
What is the best / efficient method I can use to pass strings between the two libraries. 我可以用来在两个库之间传递字符串的最佳/有效方法是什么。
I am currently on Windows using Visual C++ v9 Express but would prefer a portable solution. 我目前在Windows上使用Visual C ++ v9 Express,但更喜欢便携式解决方案。
Assuming you mean UTF-16 and not UTF-8 for std::wstring
, you will have to encode/decode the strings from one library to the other. 假设你的意思是UTF-16而不是
std::wstring
UTF-8,你必须将字符串从一个库编码/解码到另一个库。 I'm not sure if/what the STL provides for that, but you can use Windows's own MultiByteToWideChar()
and WideCharToMultiByte()
functions to convert between UTF-8 and UTF-16 with just a few lines of code. 我不确定STL是否为此提供了什么,但您可以使用Windows自带的
MultiByteToWideChar()
和WideCharToMultiByte()
函数,只需几行代码即可在UTF-8和UTF-16之间进行转换。 You could then wrap that into your own functions so you can replace the logic when you find something more portable, eg: 然后,您可以将其包装到您自己的函数中,这样您就可以在找到更便携的东西时替换逻辑,例如:
std::wstring Utf8ToUtf16(const std::string &s)
{
std::wstring ret;
int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.length(), NULL, 0);
if (len > 0)
{
ret.resize(len);
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.length(), const_cast<wchar_t*>(ret.c_str()), len);
}
return ret;
}
std::string Utf16ToUtf8(const std::wstring &s)
{
std::string ret;
int len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), s.length(), NULL, 0, NULL, NULL);
if (len > 0)
{
ret.resize(len);
WideCharToMultiByte(CP_UTF8, 0, s.c_str(), s.length(), const_cast<char*>(ret.c_str()), len, NULL, NULL);
}
return ret;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.