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 * const转换为const char *
- 6. 不能将'const char *'转换为'char * const *'
- 7. 转const const wchar_t *为const char *
- 8. _tcslen in Multibyte字符集:如何将WCHAR [1]转换为const char *?
- 9. C++为const char *为const char * const的
- 10. 将NSString转换为const char
- 11. 从const void转换为char?
- 12. 转换为const char *到LPCTSTR
- 13. 将NSString转换为const char *
- 14. 转换BSTR为const char *
- 15. int转换为const char *
- 16. 从`const char *'转换为`byte'
- 17. 将const char *转换为int
- 18. 将UIImage转换为const char *?
- 19. 如何 '为const char *' 转换为 'const的无符号字符*'
- 20. 自动转换为const char []为const char *的模板函数
- 21. 无法在参数传递中将'const char *'转换为'WCHAR *'
- 22. C++从LPCTSTR转换为const char *
- 23. 如何将System :: String ^转换为const char *?
- 24. 如何将'wchar_t *'转换为'const char *'
- 25. 如何将CString转换为const char *?
- 26. 如何转换CIN为const char *
- 27. 不能将Const Char [21]转换为Char?
- 28. 在C++中将char *转换为const char *
- 29. “从'const char *'无效转换为'char'[-fpermissive]”
- 30. C++将char转换为const char *
请添加说明,因为纯代码不会说太多。 – Kao
你不需要GetBuffer。 CString有一个访问内部缓冲区的LPCTSTR操作符。 – MikMik
如果'wc'是'приветмир',应该输出什么?你关心的代码页或这只是宽 - 窄转换所有宽字符是ANSI字符? –