我試圖在std::basic_string
和istringstream
的幫助下處理UTF-16字符串(放在緩衝區buf
中)。此代碼中出現異常std::bad_cast
。我的代碼有問題嗎?或者gcc的STL只能處理unsigned int
(16位)符號?std :: bad_cast當使用std :: basic_istringstream <unsigned short>
const unsigned short * buf;
// ... fiilling buf
std::basic_string<unsigned short> w(buf);
std::basic_istringstream<unsigned short> iss(w);
unsigned int result;
try { iss >> result; }
catch (std::exception& e)
{
const char * c = e.what();
}
與std::wstring
和std::wistringstream
相同的代碼工作正常。
謝謝,這個解釋。如果我有一個有效的寬字符串,恰好存儲爲'unsigned short'的數組呢?我可以以某種方式使用'std :: wstring'來處理它,而不需要手動將short數組轉換爲'wchar_t'數組?問題很簡單,在我的平臺上'wchar_t'是4個字節。 –
假設你的源數據是UTF-16編碼的,我猜想這種方法是將它轉換爲內部的'wchar_t'編碼。 –