CString output ;
const WCHAR* wc = L"Hellow World" ;
if(wc != NULL)
{
output.Append(wc);
}
printf("output: %s\n",output.GetBuffer(0));
回答
你也可以試試這個:
#include <comdef.h> // you will need this
const WCHAR* wc = L"Hello World" ;
_bstr_t b(wc);
const char* c = b;
printf("Output: %s\n", c);
_bstr_t
工具以下轉換運算符,我發現它很方便:
operator const wchar_t*() const throw();
operator wchar_t*() const throw();
operator const char*() const;
operator char*() const;
編輯:關於回答評論的說明:行const char* c = b;
導致正在創建並由_bstr_t
實例管理的字符串的窄字符副本,該實例將在銷燬時釋放一次。運算符只是返回一個指向這個副本的指針。因此,不需要複製該字符串。此外,在該問題中,CString::GetBuffer
返回LPTSTR
(即TCHAR*
)和而不是LPCTSTR
(即const TCHAR*
)。
另一種選擇是使用轉換宏:
USES_CONVERSION;
const WCHAR* wc = L"Hello World" ;
const char* c = W2A(wc);
這種方法的問題在於,對於轉換後的字符串的存儲器堆棧分配的,所以該字符串的長度是有限的。但是,這種轉換宏系列允許您選擇要用於轉換的代碼頁,如果寬字符串包含非ANSI字符,則這通常是需要的。
這是很容易的,因爲CString
僅僅是CStringT
一個typedef,你也有機會獲得CStringA
和CStringW
(你應該閱讀有關差異的文檔)。
CStringW myString = L"Hello World";
CString myConvertedString = myString;
'CStringA myConvertedString(L「Hello World」);'也可以工作 – Rost
是的,我意識到這一點,但它寫的更接近他的示例代碼。 –
此轉換對沒有匹配窄字符的寬字符做什麼? –
您可以使用sprintf
用於此目的:
const char output[256];
const WCHAR* wc = L"Hellow World" ;
sprintf(output, "%ws", wc);
我不認爲你可以將'output'聲明爲'const' – CinCout
我對Linux的
// Debian GNU/Linux 8 "Jessie" (amd64)
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
// Use wcstombs(3) to convert Unicode-string (wchar_t *) to UTF-8 (char *)
// http://man7.org/linux/man-pages/man3/wcstombs.3.html
int f(const wchar_t *wcs) {
setlocale(LC_ALL,"ru_RU.UTF-8");
printf("Sizeof wchar_t: %d\n", sizeof(wchar_t));
// on Windows, UTF-16 is internal Unicode encoding (UCS2 before WinXP)
// on Linux, UCS4 is internal Unicode encoding
for (int i = 0; wcs[i] > 0; i++) printf("%2d %08X\n",i,wcs[i]);
char s[256];
size_t len = wcstombs(s,wcs,sizeof(s));
if (len > 0) {
s[len] = '\0';
printf("mbs: %s\n",s);
for (int i = 0; i < len; i++)
printf("%2d %02X\n",i,(unsigned char)s[i]);
printf("Size of mbs, in bytes: %d\n",len);
return 0;
}
else return -1;
}
int main() {
f(L"Привет"); // 6 symbols
return 0;
}
代碼如何建立
#!/bin/sh
NAME=`basename $0 .sh`
CC=/usr/bin/g++-4.9
INCS="-I."
LIBS="-L."
$CC ${NAME}.c -o _${NAME} $INCS $LIBS
輸出
$ ./_test
Sizeof wchar_t: 4
0 0000041F
1 00000440
2 00000438
3 00000432
4 00000435
5 00000442
mbs: Привет
0 D0
1 9F
2 D1
3 80
4 D0
5 B8
6 D0
7 B2
8 D0
9 B5
10 D1
11 82
Size of mbs, in bytes: 12
- 1. 如何爲const char *轉換爲const WCHAR *
- 2. 如何在Mac OS X上將'const wchar *'轉換爲'const char *'?
- 3. 轉換 '爲const char [6]' 到「WCHAR **
- 4. 如何爲wchar_t *轉換爲const char *
- 5. 不能將'const char *'轉換爲'char * const *'
- 6. 轉const const wchar_t *爲const char *
- 7. _tcslen in Multibyte字符集:如何將WCHAR [1]轉換爲const char *?
- 8. 從`const char *'轉換爲`byte'
- 9. int轉換爲const char *
- 10. 轉換BSTR爲const char *
- 11. 將NSString轉換爲const char *
- 12. 將const char *轉換爲int
- 13. 將NSString轉換爲const char
- 14. 如何 '爲const char *' 轉換爲 'const的無符號字符*'
- 15. 自動轉換爲const char []爲const char *的模板函數
- 16. C++從LPCTSTR轉換爲const char *
- 17. 無法在參數傳遞中將'const char *'轉換爲'WCHAR *'
- 18. 如何將System :: String ^轉換爲const char *?
- 19. 如何將CString轉換爲const char *?
- 20. 如何將'wchar_t *'轉換爲'const char *'
- 21. 如何轉換CIN爲const char *
- 22. 轉換型「的char **」,以「爲const char **」
- 23. 在C++中將char *轉換爲const char *
- 24. 不能將Const Char [21]轉換爲Char?
- 25. 轉換無效從CHAR爲const char *
- 26. 將const char *轉換爲char *的問題
- 27. C++編程轉換爲const char *爲const wchart_t *
- 28. C++:無效的轉換,從'爲const char * const的「爲'字符*」
- 29. 爲什麼我不能在C中將'char **'轉換爲'const char * const *'?
- 30. 將`hana :: string`轉換爲`constexpr const char(&)[]`
請添加說明,因爲純代碼不會說太多。 – Kao
你不需要GetBuffer。 CString有一個訪問內部緩衝區的LPCTSTR操作符。 – MikMik
如果'wc'是'приветмир',應該輸出什麼?你關心的代碼頁或這只是寬 - 窄轉換所有寬字符是ANSI字符? –