开发者

Facet.narrow() uses default character where it shouldn't in utf-8

开发者 https://www.devze.com 2023-01-27 04:06 出处:网络
I have follow开发者_如何学Cing code: #include <iostream> #include <string> #include <locale>

I have follow开发者_如何学Cing code:

#include <iostream>
#include <string>
#include <locale>
#include <algorithm>

using namespace std;

int main()
{
 locale loc("cs_CZ.utf-8");
 std::wstring Str = L"aaěščřžýáíéaa";
 std::string Str2;
 const ctype<wchar_t> &ct = std::use_facet<std::ctype<wchar_t> >(loc);
 for(std::wstring::const_iterator It = Str.begin(); It < Str.end(); ++It)
   Str2 += ct.narrow(*It, '-' );
 std::cout << Str2 <<std::endl;
}

which produces this output:

xrozeh05@trakhan:/tmp$ ./a.out 
aa---------aa

But if I use cs_CZ.ISO-8859-2 as target locale, the output is correct:

xrozeh05@trakhan:/tmp$ ./a.out | iconv -f ISO-8859-2 -t utf-8
aaěščřžýáíéaa

So why doesn't it work correctly even with utf-8? I need to convert characters from wchar_t to char regardless of what encoding this particular system uses.


I beleive codecvt facet should do the trick. Ctype can only handle single byte encodings while you are trying to convert into multibyte one. Doesn't return type of ctype::narrow() method bother you?

0

精彩评论

暂无评论...
验证码 换一张
取 消