2016-08-20 8 views
2

utf8 문자를 내 std::strings에 저장하고 싶습니다. 이를 위해 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()); 

는하지만 결과는 예상과 : std::locale와 발전기를 사용하여 내 사용을 잘못 무엇

utf8_string = "Gr|ssen" 
normal_string = "Gr|ssen" 

? 는 (컴파일러 VC2015, 멀티 바이트 캐릭터)가

+0

결과를 어떻게 검사합니까? "utf8_string ="Grüssen ""을 기대하는 것은 이상합니다. 왜냐하면 본질적으로 "잘못된"디코딩을 기대하기 때문입니다. 또한, 소스 파일 인코딩은 무엇입니까? latin1이 아닌 다른 것이라면 틀립니다. – sehe

+0

VC2015 디버거로 검사 한 결과 win32 TextOutA를 사용하여 utf8에서 다시 변환 된 normal_string을 인쇄했습니다. 메모장 + +는 파일 인코딩이 ANSI임을 알려줍니다. 글쎄, utf_8 문자열을 보려면 Grüssen은 "Grüsese"가 iso8859-1을 기대하는 것으로 렌더링 할 때 utf8로 인코딩 된 Grüsse가 보이는 방식이기 때문에 "우습지 않은"문자입니다. 그래서 std :: locale에서 잘못된 점은 무엇입니까? –

답변

1

boost::locale::generator는 (동일한 인코딩 다수 로케일에 의해 사용될 수있는) 로캘 ID, 단순히 인코딩 원한다. 사용하는 구성표는 language_country.encoding이므로 de_DE.ISO-8859-15이 필요합니다.

또한 소스 코드에 비 ASCII 문자를 넣어서 놀고 있습니다. 조심해.

sync_with_stdio()에 대한 귀하의 의견도 이상합니다. 그냥 버퍼가 플러시되었는지 확인합니다.

+0

sync_with_stdio()는 C/C++ IO **에서 버퍼가/불필요하게 플러시되는지 확인하고 ** 스레드 동기화를위한 요구 사항을 제거합니다 http://en.cppreference.com/w/ cpp/io/ios_base/sync_with_stdio – sehe