windows 在cpp中将字符串转换为_T
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6814646/
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
convert string to _T in cpp
提问by yogi
I want to convert string
or char*
to the _T
but not able to do.
我想转换string
或转换char*
为_T
但无法做到。
if i write
如果我写
_tcscpy(cmdline,_T ("hello world"));
it works perfectly, but if i write
它工作得很好,但如果我写
char* msg="hello world";
_tcscpy(cmdline,_T (msg));
it shows an error like: error C2065: 'Lmsg' : undeclared identifier
它显示如下错误: error C2065: 'Lmsg' : undeclared identifier
Please give me a solution.
请给我一个解决方案。
Thanx in advance.
提前谢谢。
回答by Nawaz
_T
is a macro, defined as (if UNICODE
is defined):
_T
是一个宏,定义为(如果UNICODE
已定义):
#define _T(a) L ## a
which can work only with string-literals. So when you write _T("hi")
it becomes L"hi"
which is valid, as expected. But when you write _T(msg)
it becomes Lmsg
which is an undefined identifier, and you didn't intend that.
它只能与字符串文字一起使用。因此,当您编写_T("hi")
它时L"hi"
,它就像预期的那样有效。但是当您编写_T(msg)
它时Lmsg
,它变成了一个未定义的标识符,而您并不打算这样做。
All you need is this function mbstowcs
as:
您所需要的只是这个功能mbstowcs
:
const char* msg="hello world"; //use const char*, instead of char*
wchar_t *wmsg = new wchar_t[strlen(msg)+1]; //memory allocation
mbstowcs(wmsg, msg, strlen(msg)+1);
//then use wmsg instead of msg
_tcscpy(cmdline, wmsg);
//memory deallocation - must do to avoid memory leak!
delete []wmsg;
回答by NPE
_T
only works with string literals. All it does is turn the literal into an L""
string if the code's being compiled with Unicode support, or leave it alone otherwise.
_T
仅适用于字符串文字。L""
如果代码是在支持 Unicode 的情况下编译的,那么它所做的就是将文字转换为字符串,否则将其置之不理。
Take a look at http://msdn.microsoft.com/en-us/library/dybsewaf(v=vs.80).aspx
看看http://msdn.microsoft.com/en-us/library/dybsewaf(v=vs.80).aspx
回答by Ajay
回答by Jon
_T
is a macro that makes string literals into wide-char string literals by prepending an L
before the literal in UNICODE
builds.
_T
是一个宏,它通过L
在UNICODE
builds 中的文字前添加 an 来将字符串文字转换为宽字符字符串文字。
In other words, when you write _T("Hello")
it is as if you had written "Hello"
on an ANSI build or L"Hello"
on a UNICODE build. The type of the resulting expression is char*
or wchar_t*
respectively.
换句话说,当您编写_T("Hello")
它时,就好像您是"Hello"
在 ANSI 版本或L"Hello"
UNICODE 版本上编写的。结果表达式的类型分别为char*
或wchar_t*
。
_T
can notconvert a string variable(std::string
or char*
) to a wchar_t*
-- for this, you have to use a function like mbstowcs
or MultiByteToWideChar
.
_T
不能将字符串变量( std::string
or char*
)转换为 a wchar_t*
-- 为此,您必须使用类似mbstowcs
or的函数MultiByteToWideChar
。
Suggestion:It will be much easier for you (and in no way worse) to always make a UNICODE build and forget about _T
, TCHAR
and all other T
-derivatives. Just use wide-character strings everywhere.
建议:这将是您更容易(并且绝不更糟)总是做一个UNICODE构建和忘记_T
,TCHAR
和所有其他T
-衍生物。只需在任何地方使用宽字符串。
回答by unkulunkulu
_T
is not an actual type. It's a macro that prepends string literalswith L
so that they would be wchar_t*
s instead of char*
. If you need to convert a char*
string to wchar_t*
one at runtime, you need mbtowcsfor example.
_T
不是实际类型。这是一个宏,预先考虑字符串文字与L
这样他们就可以wchar_t*
!而非char*
。如果您需要在运行时将char*
字符串转换为一个字符串,例如wchar_t*
您需要mbtowcs。
回答by Gearoid Murphy
The _T modifier is just a declaration to tell the compiler that the string literal must be interpreted as a utf-16 encoding. The reason it doesn't work on the variable is because the contents of that variable have already been declared as ascii.
_T 修饰符只是一个声明,告诉编译器必须将字符串文字解释为 utf-16 编码。它对变量不起作用的原因是因为该变量的内容已经声明为 ascii。
As already mentioned the mbstowcsfunction is what you need to perform the conversion of a char data into utf-16 (wide char) data.
如前所述,mbstowcs函数是将 char 数据转换为 utf-16(宽字符)数据所需的函数。