std::strings
にutf8
文字を保存します。そのために私はboost::locale
変換ルーチンを使用しました。ブーストロケールジェネレータを正しく使用する
#include <boost/locale.hpp>
std::string utf8_string = boost::locale::conv::to_utf<char>("Grüssen", "ISO-8859-15");
std::string normal_string = boost::locale::conv::from_utf(utf8_string, "ISO-8859-15");
期待される結果は次のとおりです:
utf8_string = "Grüssen"
normal_string = "Grüssen"
私が使用しようとした文字列として「ISO-8859-15」を渡すことを取り除くために、予想通り、私の最初のテストのすべての作品には
代わりにstd::locale
// Create system default locale
boost::locale::generator gen;
std::locale loc=gen("ISO8859-15");
std::locale::global(loc);
// This is needed to prevent C library to
// convert strings to narrow
// instead of C++ on some platforms
std::ios_base::sync_with_stdio(false);
std::string utf8_string = boost::locale::conv::to_utf<char>("Grüssen", std::locale());
std::string normal_string = boost::locale::conv::from_utf(utf8_string, std::locale());
しかし、期待通りの結果がありません:
utf8_string = "Gr|ssen"
normal_string = "Gr|ssen"
std::locale
と発電機を使用しての私の使用して何が悪いですか? (コンパイラVC2015、文字セットマルチバイト)は
どのように結果を検査しますか? 「utf8_string = "Grüssen"」を「期待」するのは変です。本質的に、間違ったデコードを期待しているからです。また、ソースファイルのエンコードは何ですか?それがlatin1以外のものなら、それは間違っています。 – sehe
私はVC2015デバッガでそれを検査し、win32 TextOutAを使ってnormal_stringを印刷しました。これはutf8から逆変換されました。 Notepad ++はファイルのエンコーディングがANSIであることを伝えます。さて、utf_8文字列Grüssenを見るためには、 "Grüsse"はisf8-エンコードされたGrüsseが、iso8859-1を期待してレンダリングするときの見た目であるため、うまくいきません。 –