Conversion between Unicode and cstring

Source: Internet
Author: User

Before writingProgramWhen the COM component is used, it is very likely that Unicode strings and cstring are converted to each other.CodeEasy to use.

    • Unless you can confirm the type of the string to be converted, you should first use istextunicode for determination.

For example:

If (istextunicode (pdata, ldatasize, null ))

// Isuincode


// Is not Unicode

    • Unicode conversion to string

Char * pbuffer = NULL;

Long lbuffersize;

Lbuffersize = widechartomultibyte (cp_acp, wc_no_best_fit_chars, (lpcwstr) bstrstring,-1, null, 0, null, null );

Pbuffer = new char [lbuffersize + 1];

Widechartomultibyte (cp_acp, wc_no_best_fit_chars, (lpcwstr) (bstrstring),-1, pbuffer, lbuffersize + 1, null, null );

// Use pbuffer...

Delete [] pbuffer;

    • Convert string to Unicode

Int nlen = 0;

Wchar_t * punicode = NULL;

Cstring SINFO;

Nlen = multibytetowidechar (cp_acp, 0, SINFO,-1, null, 0 );

Punicode = new wchar_t [nlen + 1];

Multibytetowidechar (cp_acp, 0, SINFO,-1, punicode, nlen );


Several macros are available for ease of use.

Cstringw unicode1 = l "/x0391 and/x03a9"; // 'alpha' and 'omega'

Cstringa utf8 = cw2a (unicode1, cp_utf8 );

Const cstringw unicode2 = ca2w (utf8, cp_utf8 );

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: 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.