Wchar_t *, wchar_t, wchat_t array, Char, char *, char array, STD: String, STD: wstring, cstring ....

Source: Internet
Author: User

Reprinted from: http://kasonpt.blog.163.com/blog/static/169099697201171662830731/

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 );
}

Cstring to wchar_t

Cstring Path = "ASDF ";

Wchar_t wstr [1, 256] = path. allocsysstring ();

Or:

Wchar_t wcstring [256];

Multibytetowidechar (cp_acp, 0, path,-1, wcstring, 256 );

Wchar_t to cstring

Widechartomultibyte (cp_acp, 0, wcstring, 256,

Path. getbuffer (0), 256, null, null );

Path. releasebuffer (0 );

String to cstring
Cstring. Format ("% s", String. c_str ());

Convert Char to cstring
Cstring. Format ("% s", char *);

Char to string
String S (char *);

String to char *
Char * P = string. c_str ();

Cstring to string
String S (cstring. getbuffer ());

Cstring STR = "fdjfdas ";
String S = (lpctstr) STR;

1, string-> cstring
Cstring. Format ("% s", String. c_str ());
C_str () is indeed better than data.
2, char-> string
String S (char *);
You can only initialize it. It is best to use assign () unless it is initialized ().
3, cstring-> string
String S (cstring. getbuffer ());
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.

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.