C++相談室 part21
■ このスレッドは過去ログ倉庫に格納されています
0092デフォルトの名無しさん
NGNG昔、私がTemplateすれに貼り付けたもの。
C++ でワイド文字をマルチバイト文字列に変換
std::localeloc( "japanese" );
std::locale::global( loc );
std::wstring source( L"(・∀・)" );
std::string result( source.size()*2, char() );
const std::ctype< wchar_t >& ct = std::use_facet< std::ctype<wchar_t> >( loc );
ct.narrow( &*source.begin(),&*source.end(),'?',&result[0]);
std::wcout << L"wide : " << source << std::endl;
std::cout << "narrow : " << result << std::endl;
結果
wide : (・∀・)
narrow : (・∀・)
マルチバイト文字列からワイド文字列へは、
std::string source( "(・∀・)" );
std::wstring result( source.size(), wchar_t() );
const std::ctype< wchar_t >& ct = std::use_facet< std::ctype<wchar_t> >( loc );
ct.widen( &*source.begin(),&*source.end(),&result[0]);
VCで確認済みだが、他の環境(UNIXとか)では知らん
■ このスレッドは過去ログ倉庫に格納されています