Don't waste you precious time, now we go look it
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Let's look All data type with below table.
Ok, Let's see how to use Data Conversion
Convert CString to another type
1. Convert CString to std::string
CString cs ("Hello learn-tech-tips.blogspot.com");
CT2CA pszConvertedAnsiString(cs);
std::string strStd(pszConvertedAnsiString);
2. Convert CString to int
UpdateData(TRUE);
CString m_sString;
int a = _tstoi(m_sString);
a = a - 5;
m_sString.Format(_T("%d"), a);
UpdateData(FALSE);
3. Convert CString to big number
Using _wtoi64 or StrToInt64Ex APICString strBigNumber;
ULONGLONG nBig = _wtoi64(strBigNumber);
LONGLONG nBig;
StrToInt64Ex(L"123456789168268468", STIF_DEFAULT, &nBig);
szMsg.Format(L"%lld", nBig);
MessageBox(szMsg);
Result:
4. Convert CString to char* (char* == LPTSTR)
First way:
char* szTempString;
CString m_sString;
szTempString = (char*)(LPCTSTR)m_sString;
Another way:
char* ConvertToChar(const CString &s, char* pAnsiString, int bufsize)
{
wcstombs(pAnsiString,(LPCTSTR) s, bufsize);
return pAnsiString;
}
Another way
char* CTestDlg::ConvertCStringToChar(const CString &s)
{
int nSize = s.GetLength();
char *pAnsiString = new char[nSize+1];
memset(pAnsiString,0,nSize+1);
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}
5. Convert CString to LPTSTR
LPTSTR lptstr;
CString m_sString;
lptstr = (LPTSTR)(LPCTSTR)m_sString;
6. Convert CString to TCHAR
TCHAR szTempString;
CString m_sString;
szTempString = (TCHAR)(LPCTSTR)m_sString;
7. Convert CString to TCHAR* / wchar_t*
TCHAR* cTemp;
CString m_sString;
cTemp = (TCHAR*)(LPCTSTR)m_sString; // unicode
cTemp= (wchar_t*)(LPCTSTR)m_sString;
8. Convert CString to const wchar_t / const wchar_t*
CString message(_T("Hello huuvi168@gmail.com"));
const wchar_t *messageArray = static_cast<const wchar_t *>(message);
const wchar_t firstCharacter = message[0];
9. Convert CString to Const TCHAT[]
TCHAR szTempTest[100];
CString m_sString;
_tcscpy(szTempTest, m_sString);
10. Convert CString to const char*
CString m_sString;
const char* cstr = (LPCTSTR)m_sString;
11. Convert CString to LPCSTR
CString m_sWindowName;
LPCSTR lpcstr = (CStringA)sWindowName;
Example:
hwnd = FindWindowA(NULL, (CStringA)sWindowName);
Convert Another Type:
1. Convert double to char*
char buf [_CVTBUFSIZE];
double number;
int err = _gcvt_s ( buf, _CVTBUFSIZE, number, 5 );
if ( err != 0 )
{
printf ( "_gcvt_s failed with error code %d\n" , err);
exit(1);
}
2. Convert char* to LPCTSTR
Using mbstowcs_s APILPCTSTR szToLPCTSTR(char * szString)
{
LPTSTR lpszRet;
size_t size = strlen(szString) + 1;
lpszRet = (LPTSTR)malloc(MAX_PATH);
mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);
return lpszRet;
}
Convert another type To CString
1. Convert char* / wchar* to CStringchar* pchMessage;
CString m_strMessage;
m_strMessage = pchMessage;
2. Convert wchar_t to CStringwchar_t buffer[MAX_PATH];
CString m_sText;
m_sText = buffer;
So many data conversion in C++
All data conversion on above tips, I had used it so many times. Now I share it for all people who visit my blog for thanks.
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
Data Conversion in C++ ,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips Zidane, all things tech tips web development
- I am Zidane, See you next time soon ✋✋✋✋