2009-10-06 33 views
5

我有smalll問題,我想的Unicode轉換成多字節有什麼辦法的Unicode轉換爲多字節

+3

你檢查過wcstombs嗎? http://www.cplusplus.com/reference/clibrary/cstdlib/wcstombs/ – vpram86 2009-10-06 13:12:31

+0

我想你需要給我們更多的細節。你現在有什麼unicode格式,你想使用哪種多字節編碼? – 2009-10-06 13:12:37

回答

7
std::string NarrowString(const std::wstring& str, const char* localeName = "C") 
{ 
    std::string result; 
    result.resize(str.size()); 

    std::locale loc(localeName); 

    std::use_facet<std::ctype<wchar_t> >(loc).narrow(
    str.c_str(), str.c_str() + str.size(), '?', &*result.begin()); 

    return result; 
} 

應該使用當前區域設置將Unicode字符串轉換。對於不屬於代碼頁的字符,'?'正在使用caracter。使用Visual C++ 2005/2008進行測試。

+0

很好,但如何檢測它是否成功轉換或'?'被用於角色?往返轉換? – Narek 2012-11-20 07:55:55

3

wcstombs精美的作品對我來說:)

1

在大多數情況下,調用WideCharToMultiByte()就足夠了。

相關問題