我正在使用一個庫,並將其中一個函數std::wstring
發送給我,另一個庫需要將_TCHAR []
發送給它。我如何轉換它?如何從std :: wstring _TCHAR []轉換?
0
A
回答
2
假設你使用Unicode構建,std :: wstring.c_str()就是你需要的。請注意,c_str()保證它返回的字符串是空終止的。
例如
void func(const wchar_t str[])
{
}
std::wstring src;
func(src.c_str());
如果您使用非Unicode建立,則需要通過WideCharToMultiByte將Unicode字符串轉換爲非Unicode字符串。
0
由於@Zach鋸說,如果你只是建立統一,你可以逃脫std::wstring.c_str()
,但conteptually倒不如定義tstring
(用於std::basic_string<TCHAR>
一個typedef
),所以你可以放心地使用這種字符串完美地與預期的所有Windows和庫功能TCHAR
s 。
有關更多的樂趣,你也應該定義所有其他的字符串相關的C++設施TCHAR
S,並創建轉換功能std::string
/std::wstring
< =>tstring
。
- 其實也沒什麼編譯庫函數可以真正指望一個
TCHAR *
,因爲TCHAR
s的解析爲char
S或wchar_t
s的編譯時間,但你有這個想法。
0
使用ATL and MFC String Conversion Macros。無論您是在編譯_UNICODE
還是ANSI模式,這都可以工作。
即使不使用MFC,也可以使用這些宏。只包括本示例中顯示的兩個ATL標頭:
#include <string>
#include <Windows.h>
#include <AtlBase.h>
#include <AtlConv.h>
int main()
{
std::wstring myString = L"Hello, World!";
// Here is an ATL string conversion macro:
CW2T pszT(myString.c_str());
// pszT is now an object which can be used anywhere a `const TCHAR*`
// is required. For example:
::MessageBox(NULL, pszT, _T("Test MessageBox"), MB_OK);
return 0;
}
相關問題
- 1. 如何將std :: wstring轉換爲TCHAR *
- 2. 將CString轉換爲std :: wstring
- 3. 如何將UTF-8 std :: string轉換爲UTF-16 std :: wstring?
- 4. 如何使用std :: istringstream使用std :: wstring?
- 5. 如何將* char轉換爲_TCHAR?
- 6. 無法從'const wchar_t *'轉換爲'_TCHAR *'
- 7. 如何使用SimpleIni來返回wchar_t然後轉換爲std :: wstring?
- 8. 如何將std :: wstring轉換爲數值類型(int,long,float)?
- 9. 從Wstring轉換爲CComBstr
- 10. C++ wstring的轉換爲Unicode
- 11. 從std :: wstring Asisgn LPCWSTR數組
- 12. 將wchar_t轉換爲wstring
- 13. std :: wstring長度
- 14. 將char * argv []轉換爲wstring
- 15. C++ std :: wstring std :: string - 快速和髒轉換用作std :: map中的鍵
- 16. 如何將wchar_t *轉換爲wstring?
- 17. 如何將wstring轉換爲wchar_t *? C++
- 18. 錯誤C2664從常量的std :: string轉換爲的std :: string&
- 19. 在unicode和非unicode環境中轉換TCHAR * - > std :: wstring
- 20. 轉換C++的std :: wstring的爲UTF8與標準:: codecvt_xxx
- 21. 將std :: wstring轉換爲cstring是否安全?
- 22. 轉換爲std :: wstring的到QString的拋出鏈接錯誤
- 23. 改進System.String到std :: wstring轉換的性能?
- 24. 將BSTR轉換爲wstring
- 25. 結合std :: wstring和函數
- 26. 如何重置/清空爲std :: wstring?
- 27. 如何在python中使用std :: wstring
- 28. wstring字符串轉換器在升壓
- 29. 將wstring轉換爲WS_STRING
- 30. 將Wstring轉換爲CString
其他庫是否會接受** const ** _TCHAR []?也就是說,它是否打算改變數據?如果是這樣,可能會延長**數據嗎? – 2010-12-07 00:14:50
是的,一個const _TCHAR []沒關係。它不打算改變數據。 – coolface 2010-12-07 00:25:04