Wchar_t *, wchar_t, wchat_t array, Char, char *, char array, STD: String, STD: wstring, cstring convert to free server guard Eldest Brother

Source: Internet
Author: User
Wchar_t *, wchar_t, wchat_t array, Char, char *, char array, STD: String, STD: wstring, cstring ....
# Include <string>
// You must use MFC to use cstring and cannot contain <windows. h>
# DEFINE _ afxdll
# Include <afx. h>
Using namespace STD;
//----------------------------------------------------------------------------------
// Convert single-byte char * to wide-byte wchar *
Inline wchar_t * ansitounicode (const char * szstr)
{
Int nlen = multibytetowidechar (cp_acp, mb_precomposed, szstr,-1, null, 0 );
If (nlen = 0)
{
Return NULL;
}
Wchar_t * presult = new wchar_t [nlen];
Multibytetowidechar (cp_acp, mb_precomposed, szstr,-1, presult, nlen );
Return presult;
}
//----------------------------------------------------------------------------------
// Convert the wide byte wchar_t * to a single byte char *
Inline char * unicodetoansi (const wchar_t * szstr)
{
Int nlen = widechartomultibyte (cp_acp, 0, szstr,-1, null, 0, null, null );
If (nlen = 0)
{
Return NULL;
}
Char * presult = new char [nlen];
Widechartomultibyte (cp_acp, 0, szstr,-1, presult, nlen, null, null );
Return presult;
}
//----------------------------------------------------------------------------------
// Convert a single-character string to a wide-character wstring
Inline void ascii2widestring (const STD: string & szstr, STD: wstring & wszstr)
{
Int nlength = multibytetowidechar (cp_acp, 0, szstr. c_str (),-1, null, null );
Wszstr. Resize (nlength );
Lpwstr lpwszstr = new wchar_t [nlength];
Multibytetowidechar (cp_acp, 0, szstr. c_str (),-1, lpwszstr, nlength );
Wszstr = lpwszstr;
Delete [] lpwszstr;
}
//----------------------------------------------------------------------------------
Int _ tmain (INT argc, _ tchar * argv [])
{
Char * pchar = "I like char ";
Wchar_t * pwidechar = l "I hate wchar_t ";
Wchar_t tagwidecharlist [100];
Char CH = 'a ';
Char tagchar [100] = {null };
Cstring CSTR;
STD: String STR;

// Note: Set the language environment to output widechar
Setlocale (lc_all, "CHS ");

// Note: char * converts wchar_t *
// Note: wchar_t is not overloaded <, so cout cannot be used <output
Pwidechar = ansitounicode (pchar );
// Note: printf ("% ls") and wprintf (L "% s") are consistent
Printf ("% ls \ n", pwidechar );

// Note: wchar_t * converts wchar_t []
Wcscpy (tagwidecharlist, pwidechar );
Wprintf (L "% s \ n", tagwidecharlist );

// Note: wchar_t [] converts wchar_t *
Pwidechar = tagwidecharlist;
Wprintf (L "% s \ n", pwidechar );

// Note: Char converts string
Str. insert (Str. Begin (), ch );
Cout <STR <Endl;

// Note: wchar_t * converts string
Pwidechar = new wchar_t [Str. Length ()];
Swprintf (pwidechar, l "% s", str. c_str ());
Wprintf (L "% s \ n", pwidechar );

// Note: String Conversion char *
Pchar = const_cast <char *> (Str. c_str ());
Cout <pchar <Endl;

// Note: char * converts string
STR = STD: string (pchar );
// Note: cout <reloads the string. If printf is used, printf ("% s", str. c_str () is required ());
// But cannot print ("% s", STR); Because STR is a string class
Cout <STR <Endl;

// Note: String Conversion char []
STR = "Bored ";
Strcpy (tagchar, str. c_str ());
Printf ("% s \ n", tagchar );

// Note: String Conversion cstring;
CSTR = Str. c_str ();

// Note: cstring conversion string
STR = string (CSTR. getbuffer (CSTR. getlength ()));

// Note: char * converts cstring
CSTR = pchar;

// Note: cstring converts char *
Pchar = CSTR. getbuffer (CSTR. getlength ());

// Note: cstring converts char []
Strncpy (tagchar, (lpctstr) cstring, sizeof (tagchar ));

// Note: cstring conversion wchar_t *
Pwidechar = CSTR. allocsysstring ();
Printf ("% ls \ n", pwidechar );
}

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.