私は次のコードを持っています:
#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;
}
次の出力が生成されます。
xrozeh05@trakhan:/tmp$ ./a.out
aa---------aa
しかし、ターゲット ロケールとして cs_CZ.ISO-8859-2 を使用すると、出力は正しくなります。
xrozeh05@trakhan:/tmp$ ./a.out | iconv -f ISO-8859-2 -t utf-8
aaěščřžýáíéaa
では、utf-8 でも正しく動作しないのはなぜですか? この特定のシステムが使用するエンコーディングに関係なく、文字を wchar_t から char に変換する必要があります。