Conversion of wide character cstring and ANSI character const char * in Unicode

Source: Internet
Author: User

If you write a program in the Unicode environment today and convert the cstring to the const char * type, an error is returned: "cannot convert parameter 1 from 'wchar _ T * 'to 'const char *'". After a long search, we finally found three solutions:


First: The following method can be used in the com environment,

Cstring strsql;

Strsql. Format (_ T ('insert into class (name) values ('% s ')'),

M_strname.getbuffer (m_strname.getlength ()));

Char * pchsql = _ com_util: convertbstrtostring (strsql. getbuffer (strsql. getlength ()));


Second:Use the conversion function provided by the operating system,




Int nsize = widechartomultibyte (cp_acp,


Strsql. getbuffer (strsql. getlength ()),





False );

Char * pchstr = new char [nsize];

Widechartomultibyte (cp_acp,


Strsql. getbuffer (strsql. getlength ()),





False );

Delete pchstr;



Third: Use the conversion function provided by the C ++ Library:

# Include <stdlib. h>


Char pchsql [128] = {0 };

Wcstombs (pchsql, strsql, strsql. getlength ());



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.