_T(str) vs (CString)str? 
Author Message
 _T(str) vs (CString)str?


Probably this is a simple, silly question but...
What is the difference between _T(str) and (CString)str?

CString Ddacsae::EncodeString(CString typedText, CString key)
char plainMsgStr[MAXBUFSIZE+1];
memset(plainMsgStr, 0, MAXBUFSIZE+1);
int typedMsgLength = typedText.GetLength();
VERIFY(typedMsgLength <= MAXBUFSIZE);
memcpy(plainMsgStr, typedText.GetBuffer(typedMsgLength), typedMsgLength);

CString hex = _T("");
char plainCharBlock[BLOCKSIZE+1];
memset(plainCharBlock, 0, BLOCKSIZE+1);
memcpy(plainCharBlock, plainMsgStr+(i*BLOCKSIZE), BLOCKSIZE);
hex += _T(tempHexDblBlock);
hex += (CString)tempHexDblBlock;        
return hex;


Thanks in advance.

Sat, 29 Oct 2005 03:10:16 GMT  
 _T(str) vs (CString)str?


>What is the difference between _T(str) and (CString)str?

The _T macro is used with string literals and only string literals to select
between narrow and wide characters depending on whether or not _UNICODE is
#defined. For example:

_UNICODE not defined: _T("abc") expands to "abc"
_UNICODE defined: _T("abc") expands to L"abc"

See this MSDN topic for more:


Your (CString)str is a cast-expression which tries to convert str into a
CString. It would more typically be written as CString(str), and str can be
anything convertible to CString via a CString ctor, not just a string

Doug Harrison
Microsoft MVP - Visual C++

Sat, 29 Oct 2005 03:31:11 GMT  
 [ 2 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. Dialogs: m_myCtrl.GetWindowText(str) or GetDlgItemText(IDC_MY_CTRL, str)?

4. c standard regarding char str[ ], str[50] & *str

5. ReadString(CString& str) failure

6. char *str vs. char s

7. Initialization of structs. The old way memset(str, sizeof(str), 0) is disregarded.

8. Is i = *(int*) *str; /*char* str */ defined?

9. char *str = "a string"; str = "another string"

10. ANN: C++ Str Library updated to 2.1.2

11. Str size to use for temp string

12. Basic Question - char *str V/S int *


Powered by phpBB® Forum Software