[英]How to convert TCHAR to const char?
The most similar thing I found is conversion to char. 我发现最相似的东西是转换为char。 I'm trying to convert TCHAR "path" to const char.
我正在尝试将TCHAR“路径”转换为const char。 Btw I use character set: "Not Set".
顺便说一句,我使用字符集:“未设置”。
#include <stdlib.h>
// ... your defines
#define MAX_LEN 100
TCHAR *systemDrive = getenv("systemDrive");
TCHAR path[_MAX_PATH];
_tcscpy(path, systemDrive);
TCHAR c_wPath[MAX_LEN] = _T("Hello world!");//this is original
//TCHAR c_wPath[MAX_LEN] = path; "path" shows error
char c_szPath[MAX_LEN];
wcstombs(c_szPath, c_wPath, wcslen(c_wPath) + 1);
TCHAR is alias for different types depending of platform, defined macros, etc. so, TCHAR can be alias for char(1 byte) or WCHAR(2 bytes). TCHAR是不同类型的别名,具体取决于平台,定义的宏等。因此,TCHAR可以是char(1字节)或WCHAR(2字节)的别名。 further, WCHAR can be alias for wchar_t or unsigned short.
此外,WCHAR可以是wchar_t的别名或未签名的short。 But, you made conversion using wcstombs which has signature like
size_t wcstombs(char *, const wchar_t *, size_t)
, so you have 但是,您使用wcstombs进行了转换,其签名类似于
size_t wcstombs(char *, const wchar_t *, size_t)
,因此您拥有
char* c_szPath
pointing to converted array of char, now, if you need const char*, it is probably enough to write simply const char * myPath = c_szPath
, it is legal, and use myPath. 指向char的转换数组,现在,如果需要const char *,仅编写
const char * myPath = c_szPath
可能就足够了,这是合法的,并使用myPath。 But maybe, you even don't need this, because char* can bind on argument of type const char * if you need to pass it to function as argument. 但是也许甚至不需要,因为char *可以绑定到const char *类型的参数上,如果您需要将其传递给函数作为参数。 When you say,
当你说,
"path" shows error
“路径”显示错误
that is because array type is not assignable. 那是因为数组类型是不可分配的。 I really hope this helps.
我真的希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.