2010-06-11 39 views
0

我可以知道如何執行以下轉換嗎?字符數組到LPCTSTR

// el.strCap is char[50] 
// InsertItem is expecting TCHAR pointer (LPCTSTR) 
// How I can perform conversion? 
// I do not have access in both "list" and "el" source code 
// Hence, there is no way for me to modify their signature. 
list.InsertItem(i, el.strCap); 

而且不,我不希望使用

WideCharToMultiByte 

他們太繁瑣使用。

回答

3

如果您使用ATL,那麼你可以使用它包括various macros and helper classes做轉換:

char *test = "Hello World"; 
CA2CT ct(test); 
list.InsertItem(i, ct); 

雖然說WideCharToMultiByte太麻煩有點disingenious,在我看來。很容易打包呼叫WideCharToMultiByte並使其返回一個std :: wstring或任何你需要的。事實上,這基本上是CA2CT在幕後做...

1

如果你的字符串被編碼爲ISO-8859-1,可以很容易地轉換爲UTF-16:

// Convert an ISO-8859-1 string to a UTF-16 string 
wchar_t wstr[50]; 
for (int i = 0; i < 50; ++i) 
{ 
    wstr[i] = el.strCap[i]; 
    if (!wstr[i]) 
     break; 
} 

但如果你的數據是除ISO-8859-1之外的任何東西(或者是一個子集的ASCII),那麼你將需要處理更復雜的轉換。一旦你需要這樣做,你會發現MultiByteToWideChar相比之下並不麻煩。

1

您也可以使用CStringWlist.InsertItem(i, CStringW("blah"));