P
Peter Poulsen
Hi
I'm trying to convert a std::wstring to std::string UTF-8 encoded. I
have made a simple function that does the trick for three letters but
it can hardly be considered a generic solution.
<code>
std::string wchar2utf8(std::wstring const& wstr)
{
std::string str;
std::wstring::const_iterator itr = wstr.begin();
while(itr != wstr.end()) {
switch(*itr) {
case 0x00f8: // ø
str.push_back(0xc3);
str.push_back(0xb8);
break;
case 0x00e5: // å
str.push_back(0xc3);
str.push_back(0xa5);
break;
case 0x00e6: // æ
str.push_back(0xc3);
str.push_back(0xa6);
break;
default:
str.push_back(*itr);
break;
}
++itr;
}
return str;
}
</code>
I have tried google but it is extremely hard to find a good
explanation of how to do it. Is there somebody that can give an
example of a generic solution for my wchar2utf8() function?
Yours
/peter
I'm trying to convert a std::wstring to std::string UTF-8 encoded. I
have made a simple function that does the trick for three letters but
it can hardly be considered a generic solution.
<code>
std::string wchar2utf8(std::wstring const& wstr)
{
std::string str;
std::wstring::const_iterator itr = wstr.begin();
while(itr != wstr.end()) {
switch(*itr) {
case 0x00f8: // ø
str.push_back(0xc3);
str.push_back(0xb8);
break;
case 0x00e5: // å
str.push_back(0xc3);
str.push_back(0xa5);
break;
case 0x00e6: // æ
str.push_back(0xc3);
str.push_back(0xa6);
break;
default:
str.push_back(*itr);
break;
}
++itr;
}
return str;
}
</code>
I have tried google but it is extremely hard to find a good
explanation of how to do it. Is there somebody that can give an
example of a generic solution for my wchar2utf8() function?
Yours
/peter