C++ 如何将 char* 转换为 TCHAR[]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16342976/
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 char* to TCHAR[ ]?
提问by parth patel
char* stheParameterFileName = argv[1]; //I'm passing the file name as a parameter.
TCHAR szName [512];
How can I convert char*
to TCHAR []
?
我怎样才能转换char*
为TCHAR []
?
回答by John Sibly
If you include the header file:
如果包含头文件:
#include "atlstr.h"
Then you can use the A2T macro as below:
然后您可以使用 A2T 宏,如下所示:
// You'd need this line if using earlier versions of ATL/Visual Studio
// USES_CONVERSION;
char* stheParameterFileName = argv[1];
TCHAR szName [512];
_tcscpy(szName, A2T(stheParameterFileName));
MessageBox(NULL, szName, szName, MB_OK);
回答by bash.d
Form MSDN:
表单MSDN:
// convert_from_char.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
// Create and display a C style string, and then use it
// to create different kinds of strings.
char *orig = "Hello, World!";
cout << orig << " (char *)" << endl;
// newsize describes the length of the
// wchar_t string called wcstring in terms of the number
// of wide characters, not the number of bytes.
size_t newsize = strlen(orig) + 1;
// The following creates a buffer large enough to contain
// the exact number of characters in the original string
// in the new format. If you want to add more characters
// to the end of the string, increase the value of newsize
// to increase the size of the buffer.
wchar_t * wcstring = new wchar_t[newsize];
// Convert char* string to a wchar_t* string.
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, orig, _TRUNCATE);
// Display the result and indicate the type of string that it is.
wcout << wcstring << _T(" (wchar_t *)") << endl;
...
}
The definition oft TCHAR
is depending on whether you are using Unicode or ANSI.
t 的定义TCHAR
取决于您使用的是 Unicode 还是 ANSI。
See also here:
另见此处:
By using the Tchar.h, you can build single-byte, Multibyte Character Set (MBCS), and Unicode applications from the same sources.
Tchar.h defines macros (which have the prefix _tcs) that, with the correct preprocessor definitions, map to str, _mbs, or wcs functions, as appropriate. To build MBCS, define the symbol _MBCS. To build Unicode, define the symbol _UNICODE. To build a single-byte application, define neither (the default).
By default, _MBCS is defined for MFC applications.
The _TCHAR data type is defined conditionally in Tchar.h. If the symbol _UNICODE
is defined for your build, _TCHAR
is defined as wchar_t;
otherwise, for single-byte and MBCS builds, it is defined as char. (wchar_t, the basic Unicode wide-character data type, is the 16-bit counterpart to an 8-bit signed char.) For international applications, use the _tcs family of functions, which operate in _TCHAR units, not bytes. For example, _tcsncpy copies n _TCHARs, not n bytes.
通过使用 Tchar.h,您可以从相同的源构建单字节、多字节字符集 (MBCS) 和 Unicode 应用程序。
Tchar.h 定义宏(具有前缀 _tcs),使用正确的预处理器定义,映射到 str、_mbs 或 wcs 函数(视情况而定)。要构建 MBCS,请定义符号 _MBCS。要构建 Unicode,请定义符号 _UNICODE。要构建单字节应用程序,请不要定义两者(默认)。
默认情况下,_MBCS 是为 MFC 应用程序定义的。_TCHAR 数据类型在 Tchar.h 中有条件地定义。如果_UNICODE
为您的构建定义了符号,_TCHAR
则定义为wchar_t;
否则,对于单字节和 MBCS 构建,它被定义为 char。(wchar_t 是基本的 Unicode 宽字符数据类型,是 8 位有符号字符的 16 位对应物。)对于国际应用程序,请使用 _tcs 系列函数,它以 _TCHAR 单位而不是字节进行操作。例如,_tcsncpy 复制 n 个 _TCHAR,而不是 n 个字节。
回答by brian beuning
Your project may be setup to use Unicode. Unicode is for programs that want to handle most languages on planet earth. If you do not need this, go to project properties / general / character set and switch from Unicode to multi-byte.
您的项目可能设置为使用 Unicode。Unicode 适用于想要处理地球上大多数语言的程序。如果您不需要这个,请转到项目属性/常规/字符集并从 Unicode 切换到多字节。