简体   繁体   English

将CString隐式转换为char *

[英]Implicit convert CString to char*

I have downloaded an sample code, so there are some CString variables in that code which are passed to the sscanf() function as char* the compiler implicitly converts those CString and the code complie fine.the code which works fine is here: 我已经下载了一个示例代码,因此在该代码中有一些CString变量作为char *传递给sscanf()函数,编译器隐式转换那些CString并且代码编译正常。这里的代码工作正常:

CString m_strVersionXFS;
m_strVersionXFS = _T("00029903");

DWORD nVersion;
sscanf(m_strVersionXFS,"%08X",&nVersion);

the problem is here when i tried to write my own simple code which tries to manipulate a CString variable in the same way but the compiler says which can't convert a CString to a cahr* 当我试图编写我自己的简单代码试图以相同的方式操纵CString变量但编译器说不能将CString转换为cahr时问题就在这里

I suspect that your own code is using unicode ( UNICODE constant defined). 我怀疑你自己的代码使用的是unicode( UNICODE常量定义)。 This means that CString is using wide characters, and will implicitly convert to wchar_t* , but not to char* . 这意味着CString使用宽字符,并将隐式转换为wchar_t* ,但不会转换为char*

If that is the case, there are three possible solutions: 如果是这种情况,有三种可能的解决方案:

  1. Use swscanf , the wide character version of sscanf (recommended); 使用swscanfsscanf的宽字符版本(推荐);
  2. Explicitly use CStringA instead of CString so you're using the ANSI version of CString ; 明确使用CStringA而不是CString因此您使用的是CString的ANSI版本;
  3. Change your project to use multi-byte characters rather than unicode (this can be done from the project properties in Visual Studio). 将项目更改为使用多字节字符而不是unicode(可以从Visual Studio中的项目属性中完成)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM