2

我有以下代码:

#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。

4

1 回答 1

2

我相信 codecvt 方面应该可以解决问题。当您尝试转换为多字节编码时,Ctype 只能处理单字节编码。ctype::narrow() 方法的返回类型不会打扰您吗?

于 2010-11-25T20:10:10.497 回答