Convert CString to ANSI string in UNICODE projects

Source: Internet
Author: User

Convert CString to ANSI string in UNICODE projects

Quick Answer:use an intermediate CStringA.

  • Normally, this was not something that should was done. *it is technically unreliable, unless you can guarantee that the source CString to be converted does no contain any 2-byt E characters.
  • This would work fine if is using the Chinese language without any special 2-byte symbols or accented letters.
  • This article are for educational use, and explains how it can easily be do, without relying on the USES_CONVERSION macro With W2A, or ridiculous widechartomultibyte API functions.
  • If you is using a language that actually requires Unicode (Asian languages, etc), or if the source CString contains any 2 -byte character, this cannot is done. This is because there are no ANSI equivalent of any 2-byte character.
  • It is the responsibility of the programmer to ensure, the source CString does not contain any 2-byte characters.

    Use intermediate CStringA (highly recommended):

  • Pros:this is the easiest to use.
  • cons:you cannot specify a code page.

    CString Lastnamew(L"Smith");

    CStringA Lastnamea(lastnamew);

    Functionforansi(lastnamea. GetString());

  • Or An even simpler example:

    CString Lastnamew(L"Smith");

    Functionforansi(cstringa(lastnamew). GetString());

    Here is some other ways this either do not work or is not recommended. I list them to the document things to avoid. What does not.

    WideCharToMultiByte API function (not recommended):

  • Pros:you can specify the desired code page.
  • cons:too Much code to write, test, Debug.

    CString Lastnamew(L"Smith");

    int Nlen =widechartomultibyte (cp_acp, 0,< Span style= "COLOR: #999999" > (lpcwstr) lastnamew, -1,, Null

    lpstr lpsza =new CHAR [nlen];

    widechartomultibyte (cp_acp, 0,< Span style= "COLOR: #999999" > (lpcwstr) lastnamew, -1, Lpsza , Nlen

    Functionforansi(lpsza);

    Delete[] lpsza; Free the string

    W2A ATL 3.0 macros (not recommended):

  • Cons:not safe inside loops.
  • cons:you cannot specify a code page.

    Uses_conversion;

    CString Lastnamew(L"Smith");

    Functionforansi(w2a(lastnamew. GetString()));

    CW2A ATL 7.0 Conversion template classes (not recommended):

  • There is 3 ways you can use the CW2A template class. Only one of them are the right.
  • Cons:too difficult to remember the correct usage
  • Cons:too Easy-to-use improperly.

    CString Lastnamew(L"Smith");

    CW2A psza(lastnamew. GetString()); the right and the right

    Functionforansi(psza);

    CString Lastnamew(L"Smith");

    Functionforansi(cw2a(lastnamew. GetString())); //Improper usage, do

    CString Lastnamew(L"Smith");

    LPCSTR psza =cw2a(lastnamew. GetString()); //Improper usage, do

    Functionforansi(psza);

    (LPCSTR) (LPCTSTR) Cast:

  • Do not use this!
  • You cannot use (LPCSTR) (LPCTSTR) to cast a CString to LPCSTR in Unicode projects.
  • It does compile, but it does not properly convert the CString to an LPCSTR.
  • The resulting string would either be 0 or 1 length, or filled with garbage characters of unknown length, because the cast j UST changes the pointer type without any conversion.
  • You end up with a char* pointing to a WCHAR array, and a very bad thing.

    CString LastName(L"Smith");

    Functionforansi((LPCSTR) (lpctstr)LastName); Improper usage, do not

    REF:

    ATL String:what ' s wrong with the uses_conversion macros? How to avoid using them?

    Using MFC Mbcs/unicode Conversion Macros

    ATL and MFC String Conversion Macros

    CString Management

Convert CString to ANSI string in UNICODE projects

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.