在std :: wstring和std :: string之间处理UTF-8编码的字符串 [英] Handling UTF-8 encoded strings between std::wstring and std::string
问题描述
我使用两个库,一个在 std :: wstring
中存储UTF-8字符串,在 std中存储另一个字符串(UTF- :: string
。
我可以用来在两个库之间传递字符串的最好/有效的方法是什么。
我目前在Windows上使用Visual
I am using two libraries one that stores UTF-8 strings in std::wstring
and another stores strings ( UTF-8) in std::string
.
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.
推荐答案
假设你的意思是UTF-16,而不是UTF-8 for std :: wstring
,你必须将字符串从一个库编码/解码到另一个库。我不确定是否/什么STL提供了,但你可以使用Windows自己的 MultiByteToWideChar()
和 WideCharToMultiByte()
函数只需几行代码就可以在UTF-8和UTF-16之间进行转换。然后你可以将它包装到你自己的函数中,所以当你找到更便携的东西,你可以替换逻辑,例如:
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. 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. 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;
}
这篇关于在std :: wstring和std :: string之间处理UTF-8编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!