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 m_strVersionXFS;
m_strVersionXFS = _T("00029903");
DWORD nVersion;
sscanf(m_strVersionXFS,"%08X",&nVersion);
the problem is here when i tried to 开发者_StackOverflow社区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*
I suspect that your own code is using unicode (UNICODE
constant defined). This means that CString
is using wide characters, and will implicitly convert to wchar_t*
, but not to char*
.
If that is the case, there are three possible solutions:
- Use
swscanf
, the wide character version ofsscanf
(recommended); - Explicitly use
CStringA
instead ofCString
so you're using the ANSI version ofCString
; - Change your project to use multi-byte characters rather than unicode (this can be done from the project properties in Visual Studio).
精彩评论