Convert from TCHAR* to char*
-
I am writing an application that communicates with an external device. I need to copy a CString (TCHAR*) to a multibyte string buffer (char*) - the catch is I need the code to be able to compile with UNICODE on or off. Basically I need a function that can copy either a WCHAR* or char* to a char*. Currently I have:
CString szVal = _T("Some User Input String"); // i.e. not always the same value
char* Buf;
size_t BufLen;
#ifdef UNICODE
wcstombs_s(&szBufLen, NULL, NULL, szVal, _TRUNCATE); // get the req'd length including NULL
szBuf = new char[szBufLen];
wcstombs_s(&szBufLen, szBuf, szBufLen, szVal, _TRUNCATE); // convert and copy
#else
szBuf = new char[szVal.GetLength() + 1];
strcpy(szBuf, szVal);
#endif
// more code
delete [] szBuf;The above works, but I would prefer not having to use the preprocessor directives if posssible. The function _tcscpy will not convert from WCHAR to char when needed. I know I can easily write my own overloaded function, but I was hoping there was already a function defined.
-
I am writing an application that communicates with an external device. I need to copy a CString (TCHAR*) to a multibyte string buffer (char*) - the catch is I need the code to be able to compile with UNICODE on or off. Basically I need a function that can copy either a WCHAR* or char* to a char*. Currently I have:
CString szVal = _T("Some User Input String"); // i.e. not always the same value
char* Buf;
size_t BufLen;
#ifdef UNICODE
wcstombs_s(&szBufLen, NULL, NULL, szVal, _TRUNCATE); // get the req'd length including NULL
szBuf = new char[szBufLen];
wcstombs_s(&szBufLen, szBuf, szBufLen, szVal, _TRUNCATE); // convert and copy
#else
szBuf = new char[szVal.GetLength() + 1];
strcpy(szBuf, szVal);
#endif
// more code
delete [] szBuf;The above works, but I would prefer not having to use the preprocessor directives if posssible. The function _tcscpy will not convert from WCHAR to char when needed. I know I can easily write my own overloaded function, but I was hoping there was already a function defined.
-
I am writing an application that communicates with an external device. I need to copy a CString (TCHAR*) to a multibyte string buffer (char*) - the catch is I need the code to be able to compile with UNICODE on or off. Basically I need a function that can copy either a WCHAR* or char* to a char*. Currently I have:
CString szVal = _T("Some User Input String"); // i.e. not always the same value
char* Buf;
size_t BufLen;
#ifdef UNICODE
wcstombs_s(&szBufLen, NULL, NULL, szVal, _TRUNCATE); // get the req'd length including NULL
szBuf = new char[szBufLen];
wcstombs_s(&szBufLen, szBuf, szBufLen, szVal, _TRUNCATE); // convert and copy
#else
szBuf = new char[szVal.GetLength() + 1];
strcpy(szBuf, szVal);
#endif
// more code
delete [] szBuf;The above works, but I would prefer not having to use the preprocessor directives if posssible. The function _tcscpy will not convert from WCHAR to char when needed. I know I can easily write my own overloaded function, but I was hoping there was already a function defined.
What about
T2A()
macro? Refer it here - http://msdn.microsoft.com/en-us/library/87zae4a3(VS.80).aspx[^] Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
x87Bliss wrote:
but I was hoping there was already a function defined.
Well while you were hoping, I was searching.[^] :rolleyes:
led mike