C++ 如何将 const WCHAR * 转换为 const char *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12637779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert const WCHAR * to const char *
提问by Hyman
CString output ;
const WCHAR* wc = L"Hellow World" ;
if( wc != NULL )
{
output.Append(wc);
}
printf( "output: %s\n",output.GetBuffer(0) );
回答by Zdeslav Vojkovic
you can also try this:
你也可以试试这个:
#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
implements following conversion operators, which I find quite handy:
_bstr_t
实现以下转换运算符,我觉得这很方便:
operator const wchar_t*( ) const throw( );
operator wchar_t*( ) const throw( );
operator const char*( ) const;
operator char*( ) const;
EDIT: clarification with regard to answer comments: line const char* c = b;
results in a narrow character copy of the string being created and managed by the _bstr_t
instance which will release it once when it is destroyed. The operator just returns a pointer to this copy. Therefore, there is no need to copy this string. Besides, in the question, CString::GetBuffer
returns LPTSTR
(i.e. TCHAR*
) and notLPCTSTR
(i.e. const TCHAR*
).
编辑:关于回答评论的澄清:行const char* c = b;
导致由_bstr_t
实例创建和管理的字符串的窄字符副本,当它被销毁时将释放一次。运算符只返回一个指向该副本的指针。因此,无需复制此字符串。此外,在问题中,CString::GetBuffer
返回LPTSTR
(ie TCHAR*
) 而不是LPCTSTR
(ie const TCHAR*
)。
Another option is to use conversion macros:
另一种选择是使用转换宏:
USES_CONVERSION;
const WCHAR* wc = L"Hello World" ;
const char* c = W2A(wc);
The problem with this approach is that the memory for converted string is allocated on stack, so the length of the string is limited. However, this family of conversion macros allow you to select the code page which is to be used for the conversion, which is often needed if wide string contains non-ANSI characters.
这种方法的问题是转换后的字符串的内存是在栈上分配的,所以字符串的长度是有限的。但是,这一系列转换宏允许您选择用于转换的代码页,如果宽字符串包含非 ANSI 字符,则通常需要这样做。
回答by l0pan
You can use sprintf
for this purpose:
您可以sprintf
为此目的使用:
const char output[256];
const WCHAR* wc = L"Hellow World" ;
sprintf(output, "%ws", wc );
回答by Alexander Lubyagin
My code for Linux
我的 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] = '#!/bin/sh
NAME=`basename $ ./_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
.sh`
CC=/usr/bin/g++-4.9
INCS="-I."
LIBS="-L."
$CC ${NAME}.c -o _${NAME} $INCS $LIBS
';
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;
}
How to build
如何构建
std::wcout << L"output: " << output.GetString() << std::endl;
Output
输出
CStringW myString = L"Hello World";
CString myConvertedString = myString;
回答by Luchian Grigore
You could do this, or you could do something cleaner:
你可以这样做,或者你可以做一些更清洁的事情:
##代码##回答by Mark Ingram
It's quite easy, because CString
is just a typedef for CStringT
, and you also have access to CStringA
and CStringW
(you should read the documentation about the differences).
这很容易,因为CString
它只是一个 typedef for CStringT
,并且您还可以访问CStringA
和CStringW
(您应该阅读有关差异的文档)。