Vc type conversion functions, vc Functions

Source: Internet
Author: User

Vc type conversion functions, vc Functions

 

There are various types in windows c ++. in actual application, the types also need to be converted to each other. Therefore, conversions between common types are sorted and encapsulated into functions for reference only, please correct me if anything is wrong! **************************************** * ****************************** 1. function: toInteger description: convert CString to int *********************************** * ********************************** int ToInteger (CString csSource) {if (csSource. trim () = _ T ("") {return-1;} int nResult = _ ttoi (csSource); return nResult ;} ****************************************: toString description: convert int to CString *********************************** * ********************************** bHex indicates whether to convert hexadecimal CString ToString (int nSource, BOOL bHex) {if (bHex) {strResult. format (_ T ("% 02x"), nSource);} else {strResult. format (_ T ("% d"), nSource);} return strResult ;} **************************************** * ****************************** 3. functions: toString Description: Convert int to CString. Note: delete
**************************************** ***************************** Void ToPChar (LPWSTR pszSource, char * & pcrdest) {int nsourceLen = WideCharToMultiByte (CP_ACP, 0, pszSource,-1, NULL, 0, NULL); if (nsourceLen = 0) {AfxMessageBox (_ T ("Empty input value! "); Return;} pcrdest = new char [nsourceLen + 1]; memset (pcrdest, 0x00, sizeof (char) * (nsourceLen + 1 ));:: wideCharToMultiByte (CP_ACP, 0, pszSource,-1, pcrdest, nsourceLen, NULL, NULL );} **************************************** * ****************************** 4. functions: no description: CString to char ************************************* * ********************************* # include <atlconv. h> USES_CONVERSION; char * cMac = T2A (csCommunitation ); **************************************** * ****************************** 4. functions: no description: char * To wchar ************************************ *********************************** ① # include <atlconv. h> USES_CONVERSION; char * c = "Hello"; char * cMac = A2W (c); ② wchar_t ws [100]; swprintf (ws, 100, L "% hs ", "ansi string"); ③ const wchar_t * GetWC (const char * c) {const size_t cSize = strlen (c) + 1; wchar_t * wc = new wchar_t [cSize]; mbstowcs (wc, c, cSize); return wc ;} **************************************** * ****************************** 4. functions: no description: wchar * To char ************************************ * ********************************① USES_CONVERSION; WCHAR * wc = L "Hello World"; char * c = W2A (wc); ② char output [256]; WCHAR * wc = L "Hellow World"; sprintf (output, "% ws", wc); ③ # include <comdef. h> // you will need this const WCHAR * wc = L "Hello World"; _ bstr_t B (wc); const char * c = B; printf ("Output: % s \ n ", c ); **************************************** * ****************************** 5. functions: convert description: convert a pointer string to an unsigned pointer string ******************************* * ************************************ void convert (const char * szStr, unsigned char * pBuf, int nBufSize) {char szTemp [3] = {0}; int nLen = strlen (szStr); for (int I = 0, j = 0; I <nLen-1, j <nBufSize; I + = 2, j ++) {szTemp [0] = szStr [I]; szTemp [1] = szStr [I + 1]; pBuf [j] = (unsigned char) strtoul (szTemp, NULL, 16 );}} **************************************** * 6: CStringToUnsignedChar Description: converts a CString to an unsigned char * header file: # include <atlconv. h> ************************************** * ***************************** unsigned char * CStringToUnsignedChar (CString csCommunitation, unsigned char * ucBuf) {USES_CONVERSION; char * cMac = T2A (csCommunitation); int nLongth = strlen (cMac)/2; Convert (cMac, ucBuf, nLongth); return ucBuf ;} ****************************************: dwordToCString Description: convert an IP address to a CString. For example, convert an IP address (such as 192.168.53.75) to a CString (C0 A8 35 4B)
**************************************** * **************************** CString DwordToCString (CString csIp) {DWORD dwIp; dwIp = htonl (csIp); CString strMainServerIP; DWORD dwMainServerIP = ntohl (dwIp); // The Network order is forwarded to the host order strMainServerIP. format (_ T ("% 02X % 02X % 02X % 02X"), (dwMainServerIP> 24) & 0xff, (dwMainServerIP> 16) & 0xff, (dwMainServerIP> 8) & 0xff, dwMainServerIP & 0xff); return strMainServerIP ;} **************************************** * 8: CStringTodwIP Description: Convert the CString IP address to a dword ip address. For example, convert a CString (C0 A8 35 4B) to an IP address (for example, 192.168.53.75)
**************************************** * ***************************** DWORD CStringTodwIP (CString strIP) {DWORD dwRet = 0; CString strIPTemp = _ T (""); CString strLeft = _ T (""); CString strRight = _ T (""); int iPos = 0, iLen = 0; strIPTemp = strIP. getBuffer (0); strRight = strIPTemp; iPos = strRight. find (_ T (". "), 0); iLen = strRight. getLength (); while (iPos> 0) {strLeft = strRight. left (iPos); dwRet = (dwRet <<8) + (unsigned) _ ttoi (strLeft); strRight = strRight. right (iLen-iPos-1); iLen = strRight. getLength (); iPos = strRight. find (_ T (". "), 0);} dwRet = (dwRet <8) + (unsigned) _ ttoi (strRight); return dwRet;} (1) the difference between a short character and a long character the so-called short character is represented by 8 bits. A typical application is ASCII code, while a wide character, as the name suggests, is a character represented by 16 bits, A typical UNICODEChar is actually an unsigned char. WCHAR is a wide character, while TCHAR varies depending on whether unicode is supported. When the program uses sizeof (TCHAR), this value is 1 by default; when the UNICODE macro is defined, this value is 2 ************************************ * ********************************** 9. function: transCStringToTCHAR Description: Convert CString to TCHAR * header file: # include <windows. h> ************************************** * ****************************** TCHAR * CStringToCHAR (CString & str) {int nLen = str. getLength (); TCHAR * szRs = new TCHAR [nLen]; lstrcpy (szRs, str. getBuffer (nLen); str. rele AseBuffer (); return szRs ;} **************************************** * 10: THCARToChar description: convert TCHAR * To char ********************************** * ************************************ char * THCARToChar (TCHAR * tchStr) {int nLen = 2 * wcslen (tchStr); // CString, which is a single character in TCHAR, therefore, you do not need to calculate the length of char * chRtn = new char [nLen + 1] wcstombs (chRtn, tchStr, nLen + 1); // If the conversion is successful, return non-negative value return chRtn ;}* **************************************** * ***************************** 11. function: getAnsiString Description: converts CString (unicode) to char * (ANSI) **************************************** * ***************************** char * GetAnsiString (const CString & s) {int nSize = 2 * s. getLength (); char * pAnsiString = new char [nSize + 1]; wcstombs (pAnsiString, s, nSize + 1); return pAnsiString ;} ************************************* **************************************** **************************************** * ************************ 12. function: transCStringToTCHAR Description: Convert CString to TCHAR * header file: # include <windows. h> ************************************** * ****************************** TCHAR * CStringToCHAR (CString & str) {int nLen = str. getLength (); TCHAR * szRs = new TCHAR [nLen]; lstrcpy (szRs, str. getBuffer (nLen); str. releaseBuffer (); retu Rn szRs ;} **************************************** * ****************************** 13. functions: THCARToChar description: convert TCHAR * To char ********************************** * ************************************ char * THCARToChar (TCHAR * tchStr) {int nLen = 2 * wcslen (tchStr); // CString, which is a single character in TCHAR, therefore, you do not need to calculate the length of char * chRtn = new char [nLen + 1] wcstombs (chRtn, tchStr, nLen + 1); // If the conversion is successful, return non-negative value return chRtn ;} ******************* **************************************** * *********** 14. functions: getAnsiString Description: converts CString (unicode) to char * (ANSI) **************************************** * ***************************** char * GetAnsiString (const CString & s) {int nSize = 2 * s. getLength (); char * pAnsiString = new char [nSize + 1]; wcstombs (pAnsiString, s, nSize + 1); return pAnsiString ;} **************************************** *************** * **************** 15. Description: convert const char * To CString ********************************* * ********************************** CString TransLPCSTRToCString (const char *) {int len = strlen (pErrMsg); TCHAR * c1 = (TCHAR *) malloc (sizeof (TCHAR) * len); MultiByteToWideChar (CP_ACP, 0, pErrMsg, len + 1, c1, len + 1); csText. format (L "% s", pErrMsg); retrun csText ;} **************************************** ************* * 16. function: CStringToLPCSTR description: convert CString to const char ********************************** * ******************** const char * CStrToLPCTSTR (CString strSrc) {DWORD dwNum = WideCharToMultiByte (CP_OEMCP, NULL, strSrc. getBuffer (0),-1, NULL, 0, NULL, FALSE); char * psText; psText = new char [dwNum]; if (! PsText) delete [] psText; WideCharToMultiByte (CP_OEMCP, NULL, strSrc. getBuffer (0),-1, psText, dwNum, NULL, FALSE); return (const char *) psText ;} **************************************** * ************* 17. functions: WCharToUTF8 Description: Convert const TCHAR * (LPCTSTR) to CHAR * (LPTSTR) **************************************** * *************** std:: string WCharToUTF8 (LPCTSTR wszBuffer) {UINT nLen = WideCharToMultiByte (CP_UTF8, 0, wszBuffer,-1, NULL, 0, NULL, NULL ); CHAR * szAscii = new CHAR [nLen + 1]; WideCharToMultiByte (CP_UTF8, NULL, wszBuffer,-1, szAscii, nLen, NULL, NULL); std: string ret = szAscii; delete [] szAscii; return ret ;} **************************************** * ************* 18. function: UTF8ToChar Description: Convert CHAR * (LPTSTR) to const TCHAR * (LPCTSTR) **************************************** * *************** std:: string UTF8ToChar (LPCSTR szUTF8) {UINT nLen = MultiByteToWideChar (CP_UTF8, NULL, szUTF8,-1, NULL, NULL); WCHAR * wszBuffer = new WCHAR [nLen + 1]; nLen = MultiByteToWideChar (CP_UTF8, NULL, szUTF8,-1, wszBuffer, nLen); wszBuffer [nLen] = '\ 0'; nLen = WideCharToMultiByte (CP_ACP, 0, wszBuffer, -1, NULL, 0, NULL, NULL); CHAR * szAscii = new CHAR [nLen + 1]; WideCharToMultiByte (CP_ACP, NULL, wszBuffer,-1, szAscii, nLen, NULL, NULL); szAscii [nLen] = '\ 0'; std: string ret = szAscii; // clear the memory delete [] wszBuffer; delete [] szAscii; return ret ;} **************************************** * ****************************** 19. Convert CString to LPWSTR (that is, wchar _*) USES_CONVERSION; LPWSTR lpwStr = A2W (m_csUserName ); **************************************** **************************************** **************************************** * ******************* 20. Convert CString to LPWSTR (that is, wchar _*) USES_CONVERSION; LPWSTR lpwStr = A2W (m_csUserName ); **************************************** ******************************

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.