Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Data Conversion in C++

Tuesday 8 September 2015
|
Read: Completed in minutes

[Tips] Data Conversion in C++

Data Conversion in C++ is one of technical skill for C++ developer. These technique is not complex if you understand it. But complex with you don't know it :)
Don't waste you precious time, now we go look it



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 API

CString 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 API

LPCTSTR 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 CString
char* pchMessage;
CString m_strMessage;
m_strMessage = pchMessage;
2. Convert wchar_t to CString
wchar_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 ✋✋✋✋


🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
  • I enjoy learning new technologies, researching, and upgrading my current frameworks to the latest versions for better performance and security
  • I have strong skills in both backend and frontend development, with more advanced skills in backend. For backend, I'm good at PHP (especially CAKEPHP), NodeJS / NestJS, .NET C# (4.5), ASP.NET, Webservice, C++ 11, and lua script 4.0 5.0.
  • For backend, I'm good at PHP (especially CAKEPHP), NodeJS / NestJS, .NET C# (4.5), ASP.NET, Webservice, C++ 11, and lua script 4.0 5.0.
  • For frontend, I can work with HTML, CSS (TailwindCSS, bootstrap), Javascript ES6, JQuery, JS Framework, and NextJS
  • For database, I can work well with Mysql, SQL Server, PostgreSQL, and TypeORM.
  • Experienced working with RemoteThread, ASM byte code, Send Package, Send Key
  • I have experienced in using Aliyun Cloud Service (https://www.alibabacloud.com/): ECS, RDS (postgresQL).
  • I have experienced in building docker compose file: SQL, Redis, PHP8: https://learn-tech-tips.blogspot.com/2022/09/how-to-create-a-dockerfile.html
  • I have experienced in integrating with various payment gateways such as Paypal, Payme, AsiaPay, HANG SENG NTT Payment gateway (using CryptoJS.enc.Base64.stringify / base64_encode; CryptoJS.enc.Hex.stringify / bin2hex; CryptoJS.HmacSHA256 / hash_hmac, etc.)
  • I have used various technologies such as Restful API, WebService, Process, WindowForm, Window Message, GraphQL, Network, Multi-thread, I/O Read Write File, Synchronous and Asynchronous, Inject DLL, Hook, Window Message, File, MFC, Firebase Push Notification, JWT authentication
  • I have knowledge of obfuscation .net source code and reverse engineering.
  • I understand basic Assembly language (ASM) to compile engine code.
  • I can create websites and single page applications with PHP (Cakephp 2+/ 4+ framework)
  • I can use shell script in Linux.
  • I have basic skills in Android for Java and Swift IOS development
  • I have experienced in uploading apps to Google Play.
  • I can use Photoshop for image editing, coloring, background decoration, banner, logo, etc.
  • I have experienced in using Figma for design.
  • I can manage source code with Git and SVN.
  • I can communicate well in written English.
  • I can communicate well in spoken/written Chinese (Cantonese/mandarin).
  • I can set up servers using VPS (Virtual Private Server) and use Nginx for config balancing (Ip_hash, least_conn), IIS, Apache.
  • ...
  • This is a brief introduction of myself and my skills. If you want to know more about me or my projects, please feel free to contact me or visit my blog. Thank you for reading
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane