2014-03-19 101 views
1

我已經編程了很多年,但對std命名空間和std::string類非常新。將char *轉換爲Unicode std :: string

我在寫代碼來讀取Gdiplus::PropertyItem::value的值,即char *

什麼是最接受的方式來將此char *值轉換爲string,在我的情況下,這是一個Unicode字符串?

+0

看到http://utf8everywhere.org –

回答

3

你提到string但你說它是一個Unicode字符串。那麼我想你的意思是wstring。您可以使用MultiByteToWideChar函數在兩者之間進行轉換。事情是這樣的:

std::string str(...); 
int size = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); 
std::wstring wstr(size, 0); 
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstr[0], size); 
+0

對不起,我已經習慣了MFC和'CString',這是ASCII或Unicode取決於構建設置。所以,是的,它看起來像我需要'std :: wstring'。但我很失望,我需要使用原始的Windows API,而'std :: wstring'在這裏甚至不提供幫助。 –

+0

@JonathanWood wstring vs string。看看http://stackoverflow.com/a/402918/1866301 –

相關問題