Various types of conversions under Unicode, cstring,string,char*,int,char[]

Source: Internet
Author: User

Summarize the various types of Unicode conversions that have recently been used, and encounter additional additions in the future:

1. String Turn CString

String a= "abc";

CString str=cstring (A.c_str ());

Or Str.format ("%s", A.c_str ())

2, int turn CString

Int A;

CString Cstr;

Cstr.format (_t ("%d"), a);

3, Char turn CString

Cstring.format ("%s", char*);

Cases:

Char szpath[];

CString Cstr;

Cstr.format (_t ("%s"), szpath);

4, CString turn string
CString c_str=_t ("abc");

String str ((LPCSTR) CStringA (C_STR));

or string Str=cstringa (C_STR);

or transit via char*

CString m_str (_t ("Qwerg"));

Char *chr=new char[m_str. GetLength () +1];

WideCharToMultiByte (cp_acp,0,m_str. GetBuffer (), -1,chr,m_str. GetLength () +1,null,null);

String STR=CHR;

or CString c_str = _t ("Ooqoqoq");

SetLocale (Lc_all, "CHS");

Char *p = new Char[c_str. GetLength () +1];

Wcstombs (p,c_str,c_str. GetLength () +1);

string str = p;

Use the following code to detect:

MessageBox ((CString) str.c_str ());

5, CString turn const char*

CString a=_t ("");

CStringA B (a);

const Char *c=new char (50);

C=b.getstring ();

Or:

CString filepath=_t ("");

int Len=widechartomultibyte (cp_acp,0,filepath.allocsysstring (), Filepath.getlength (), null,0,null,null);

char * pascii =new Char[len];

Len=widechartomultibyte (Cp_acp,0,filepath.allocsysstring (), Filepath.getlength (), pascii,len+1,null,null);

pascii[len]=0;

Const char* path= (const char*) pascii;

6, CString turn char*

CString strpath = _t ("Ah ah ah");

int nlength = Strpath.getlength ();

int nbytes = WideCharToMultiByte (cp_acp,0,strpath,nlength,null,0,null,null);

char* Voicepath = new char[nbytes + 1];

memset (voicepath,0,nlength + 1);

WideCharToMultiByte (CP_OEMCP, 0, strpath, nlength, Voicepath, nbytes, NULL, NULL);

Voicepath[nbytes] = 0;

7, CString turn int

CString str=_t ("123");

int B=_ttoi (str);

or turn it into a string and then int

8. Char to String
string S (char *);

9, char[] Turn LPWSTR

Direct cast

10. String Turn char*

There are 3 ways to convert a string to char*:
1.data
Such as:
String str= "abc";
Char *p=str.data ();
2.c_str
such as: string str= "GDFD";
Char *p=str.c_str ();
3. Copy
Like what
String str= "Hello";
Char p[40];
Str.copy (p,5,0); Here 5, representing a copy of a few characters, 0 for the location of the copy
* (p+5) = ' + '; To manually add a terminator
cout <</span> <</span>p;

11. String to int

String num;

int A=atoi (NUM.C_STR ());

12,int turn string

#include

StringStream SS;

int n = 123;
String str;
ss<<n;
ss>>str;

Various types of conversions under Unicode, cstring,string,char*,int,char[]

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.