Mutual conversion between cstring & cstringa & cstringw

Source: Internet
Author: User

During MFC programming, the conversion between cstring, cstringa, and cstringw is often encountered, that is, the conversion between chat string and wchar_t string.

Now I have written a function for mutual conversion between them. The Code is as follows:

Header file cstringtoolex. h

# Ifndef _ cstring_tool_ex _ # DEFINE _ cstring_tool_ex _ # include <cstringt. h> /// cstring to cstringa // # ifndef cstrt2cstra # ifdef _ Unicode # define cstrt2cstra (CSTR) cstrw2cstra (CSTR) # else # define cstrt2cstra (CSTR) (CSTR) # endif // cstring to cstringw // # ifndef cstrt2cstrw # ifdef _ Unicode # define cstrt2cstrw (CSTR) # else # define struct (CSTR) cstra2cstrw (CSTR) # endif // cstringa to cstring // # ifndef convert # ifdef _ Unicode # define cstra2cstrt (CSTR) cstra2cstrw (CSTR )) # else # define cstra2cstrt (CSTR) # endif // cstringw to cstring // # ifndef convert # ifdef _ Unicode # define cstrw2cstrt (CSTR) # else # define compute (CSTR) cstrw2cstra (CSTR) # endif // cstringa to cstringw // cstringw cstra2cstrw (const cstringa & cstrsrca ); /// cstringw to cstringa // cstringa cstrw2cstra (const cstringw & cstrsrcw); # endif

 

Source File cstringtoolex. cpp

1 # include "stdafx. H "2 # include" cstringtoolex. H "3 # include <cstringt. h> 4 5 // 6 // cstringa to cstringw 7 // 8 cstringw cstra2cstrw (const cstringa & cstrsrca) 9 {10 int Len = multibytetowidechar (cp_acp, 0, lpcstr (cstrsrca),-1, null, 0); 11 wchar_t * wstr = new wchar_t [Len]; 12 memset (wstr, 0, Len * sizeof (wchar_t )); 13 multibytetowidechar (cp_acp, 0, lpcstr (cstrsrca),-1, wstr, Len); 14 cstringw cstrdestw = wstr; 15 Delete [] wstr; 16 17 return cstrdestw; 18} 19 20 // 21 // cstringw to cstringa22 // 23 cstringa cstrw2cstra (const cstringw & cstrsrcw) 24 {25 int Len = forward (cp_acp, 0, lpcwstr (cstrsrcw ), -1, null, 0, null, null); 26 char * STR = new char [Len]; 27 memset (STR, 0, Len); 28 widechartomultibyte (cp_acp, 0, lpcwstr (cstrsrcw),-1, STR, Len, null, null); 29 cstringa cstrdesta = STR; 30 Delete [] STR; 31 32 return cstrdesta; 33}

Example:

    CString  cstrName = TEXT("Hello Kitty");    CStringA cstraName = CStrT2CStrA(cstrName);   //CString to CStringA    CStringW cstrwName = CStrT2CStrW(cstrName);   //CString to CStringW    ::MessageBoxA(NULL, cstraName, "CStringToolEx Test", MB_OK);    ::MessageBoxW(NULL, cstrwName, L"CStringToolEx Test", MB_OK);    CStringA cstraAddr = "Anhui Anqing";    CString  cstrAddr  = CStrA2CStrT(cstraAddr);    //CStringA to CString    CStringW cstrwAddr = CStrA2CStrW(cstraAddr);    //CStringA to CStringW    ::MessageBox(NULL, cstrAddr, TEXT("CStringToolEx Test"), MB_OK);    ::MessageBoxW(NULL, cstrwAddr, L"CStringToolEx Test", MB_OK);    CStringW cstrwGender = L"Male";    CString  cstrGender  = CStrW2CStrT(cstrwGender);   //CStringW to CString    CStringA cstraGender = CStrW2CStrA(cstrwGender);   //CStringW to CStringA    ::MessageBox(NULL, cstrGender, TEXT("CStringToolEx Test"), MB_OK);    ::MessageBoxA(NULL, cstraGender, "CStringToolEx Test", MB_OK);

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.