我有以下代码:
#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。