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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:34:11  来源:igfitidea点击:

convert string to _T in cpp

c++windows

提问by yogi

I want to convert stringor char*to the _Tbut 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

_Tis a macro, defined as (if UNICODEis 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 Lmsgwhich is an undefined identifier, and you didn't intend that.

它只能与字符串文字一起使用。因此,当您编写_T("hi")它时L"hi",它就像预期的那样有效。但是当您编写_T(msg)它时Lmsg,它变成了一个未定义的标识符,而您并不打算这样做。

All you need is this function mbstowcsas:

您所需要的只是这个功能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

_Tonly 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

You need to use mbtowcsfunction.

您需要使用mbtowcs功能。

You should also look at this article.

你也应该看看这篇文章

回答by Jon

_Tis a macro that makes string literals into wide-char string literals by prepending an Lbefore the literal in UNICODEbuilds.

_T是一个宏,它通过LUNICODEbuilds 中的文字前添加 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*

_Tcan notconvert a string variable(std::stringor char*) to a wchar_t*-- for this, you have to use a function like mbstowcsor MultiByteToWideChar.

_T不能将字符串变量( std::stringor char*)转换为 a wchar_t*-- 为此,您必须使用类似mbstowcsor的函数MultiByteToWideChar

Suggestion:It will be much easier for you (and in no way worse) to always make a UNICODE build and forget about _T, TCHARand all other T-derivatives. Just use wide-character strings everywhere.

建议:这将是您更容易(并且绝不更糟)总是做一个UNICODE构建和忘记_TTCHAR和所有其他T-衍生物。只需在任何地方使用宽字符串。

回答by unkulunkulu

_Tis not an actual type. It's a macro that prepends string literalswith Lso 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(宽字符)数据所需的函数。