CString string CStringA CStringW string wstringなどの文字列変換要約

21347 ワード

#include CString a;CStringA a1;CStringW a2;string b;wstring c;a=CA2T(b.c_str());//b->ab=CT2A((LPCTSTR)a);//a->ba=b.c_str();//c->ab=(LPCTSTR)a;//a->ca1=b.c_str();//b->a1b=(LPCSTR)a1;//a1->ba1=CW2A(b.c_str());//c->a1b=CA2W((LPCSTR)a1);//a1->ca2=CA2W(b.c_str());//b->a2b=CW2A((LPCWSTR)a2);//a2->ba2=b.c_str();//c->a2b=(LPCWSTR)a2;//a2->c
.メモリが割り当てられていないconst char*(LPCTSTR)ポインタ.CString cstr(asdd);const char* ch = (LPCTSTR)cstr; chが指すアドレスはcstrと同じです.ただしconstを使用することでchが修正されないことを保証するため、安全.2.メモリが割り当てられていないポインタに渡す.CString cstr = "ASDDSD"; char *ch = cstr.GetBuffer(cstr1.GetLength() + 1); cstr.ReleaseBuffer();//chが指す値を修正するのはcstrの中の値を修正するのと同じです.//PS:chを使い切るとdelete chは使いません.これはcstrの内部空間を破壊し、プログラムがクラッシュしやすいからです.二つ目の使い方.CString値を割り当てられたメモリのchar*に割り当てます.CString cstr1 = "ASDDSD"; int strLength = cstr1.GetLength() + 1; char *pValue = new char[strLength]; strncpy(pValue, cstr1, strLength);4.第三の用法CString値を割り当てられたメモリchar[]配列に割り当てる.CString cstr2 = "ASDDSD"; int strLength1 = cstr1.GetLength() + 1; char chArray[100]; memset(chArray,0, sizeof(bool) * 100);//配列のごみの内容を空にする.strncpy(chArray, cstr1, strLength1);上のいずれかでChar*に変更してStringに変更します
あなたのプロジェクトがUNICODであれば、コードの変換が重要です.
C/C++ code

   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> #include < windows.h > #include < iostream > #include < vector > using namespace std; std::wstring UT2WC( const char * buf) { int len = MultiByteToWideChar(CP_UTF8, 0 , buf, - 1 , NULL, 0 ); std::vector < wchar_t > unicode(len); MultiByteToWideChar(CP_UTF8, 0 , buf, - 1 , & unicode[ 0 ], len); return std::wstring( & unicode[ 0 ]); } std:: string WC2UT( const wchar_t * buf) { int len = WideCharToMultiByte(CP_UTF8, 0 , buf, - 1 , NULL, 0 , NULL, NULL); std::vector < char > utf8(len); WideCharToMultiByte(CP_UTF8, 0 , buf, - 1 , & utf8[ 0 ], len, NULL, NULL); return std:: string ( & utf8[ 0 ]); } std::wstring MB2WC( const char * buf) { int len = MultiByteToWideChar(CP_ACP, 0 , buf, - 1 , NULL, 0 ); std::vector < wchar_t > unicode(len); MultiByteToWideChar(CP_ACP, 0 , buf, - 1 , & unicode[ 0 ], len); return std::wstring( & unicode[ 0 ]); } std:: string WC2MB( const wchar_t * buf) { int len = WideCharToMultiByte(CP_ACP, 0 , buf, - 1 , NULL, 0 , NULL, NULL); std::vector < char > utf8(len); WideCharToMultiByte(CP_ACP, 0 , buf, - 1 , & utf8[ 0 ], len, NULL, NULL); return std:: string ( & utf8[ 0 ]); } int main() { setlocale(LC_ALL, "" ); const wchar_t * s1 = L " UNICODE UTF-8 " ; cout << WC2UT(s1).c_str() << endl; const char * s2 = " ANSI UNICODE " ; wcout << MB2WC(s2).c_str() << endl; const wchar_t * s3 = L " UNICODE ANSI " ; cout << WC2MB(s3).c_str() << endl; return 0 ; }
CString   string 
string s(CString.GetBuffer());

: :
CString,string,char* 2009-05-20 17:23CString,int,string,char*

string CString
CString.format("%s", string.c_str());

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

char string
string s(char *);

string char *
char *p = string.c_str();

CString string
string s(CString.GetBuffer());

1,string -> CString
CString.format("%s", string.c_str());
c_str() data() .
2,char -> string
string s(char *);
, assign().
3,CString -> string
string s(CString.GetBuffer());
GetBuffer() ReleaseBuffer(), .


《C++ 》
C—string
1.data(), ”/0“
2,c_str(), ”/0“
3,copy()

---------------------------------------------------------------

CString int、char*、char[100] - -


CString int、char*、char[100] - -


CString int

, atoi、_atoi64 atol。
CString , CString Format 。
CString s;
int i = 64;
s.Format("%d", i)
Format , 。

void CStrDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString
ss="1212.12";
int temp=atoi(ss);
CString aa;
aa.Format("%d",temp);
AfxMessageBox("var is " + aa);
}

sart.Format("%s",buf);

CString char*

///char * TO cstring
CString strtest;
char * charpoint;
charpoint="give string a value";
strtest=charpoint;


///cstring TO char *
charpoint=strtest.GetBuffer(strtest.GetLength());

C string,char *==char []==string

CString.Format("%s",char *) char * CString。 CString char *, (LPCSTR)CString 。


CString char[100]

char a[100];
CString str("aaaaaa");
strncpy(a,(LPCTSTR)str,sizeof(a));

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1570001


( )

string CString ,string (STL) , C++ ;

CString(typedef CStringT > CString) Visual C++ , CSimpleStringT , MFC ATL , char( ANSI),wchar_t(unicode),TCHAR(ANSI unicode );

char* C , '/0' ;

( )

string , , CString char*;

CString , , char* ;

char* , ;



char* psz = “joise”;

CString cstr( psz );

string str( cstr );

( )

a) operator=

string , , CString char*;

CString , , char* ;

char* , , strcpy memcpy, char* NULL, , ;



char *psz = NULL;

psz = new char[10]; // , char *psz = new char[10];

memset( psz, 0, 10 );

strcpy( psz, “joise” );

CString cstr;

cstr = psz;

string str;

str = psz;

str = cstr;

delete []psz;

b) operator+

string CString , char* , + , string str = str + cstr , char*;

char* + , strcat ;



char* psz = “joise”;

CString cstr = psz;

cstr = cstr + psz;

string str = psz;

str = str + str + psz;

strcat( psz, psz );

strcat( psz, cstr );//

strcat( psz, str );// , ,CString const char*, string

c) operator +=

string , +=, CString char*;

CString , += , char* ;

char* += , strcat ;

d) operator[]

CString , ;

string char* ;



char* psz = “joise”;

CString cstr = psz;

cout << cstr[8];

string str = psz;

cout << str[8];

cout << psz[8];

e) operator== 、operator!=、operator> 、operator< 、operator>= 、perator<=

CString string , char* , , ;

cout << ( psz == cstr );

cout << ( psz == str );

cout << ( str == psz );

cout << ( cstr == psz );// 1

( )

a)

char* string CString
strchr
strstr
strrstr
strspn find Find
fild_first_of FindOneOf ReserveFind find_if
:find_if true

b)

char* string CString ( ) strcmp
strncmp
strcoll
_strncoll operator<
operator>
operator<=
operator>=
operator==
operator!= Collate

Compare ( ) _stricmp
_strnicmp
_stricoll
_strnicoll CollateNoCase

CompareNoCas
: <0 ,

c)

char* string CString _strset
_strnset
replace
replace_copy
replace_copy_if
replace_if Replace
d)

char* string CString insert Insert
e) char* string CString strcat push

append Append

AppendChar

AppendFormat

f)

char* string CString substr Left

Mid

Right

Truncate

g)

char* string CString remove Remove RemoveBlanks

: ATL , C remove_if Trim

TrimLeft

TrimRig

h)

char* string CString _strlwr

_strupr MakeLower

MakeUpper

i)

char* string CString atoi

atod

atof Format char* c_str
GetBuffer

GetBufferSetLen
j)

char* string CString sprintf Format


k)

char* string CString
strlen length GetLength size GetAllocLength
l)

char* string CString ==NULL '/0' empty IsEmpty
m)

char* string CString realloc
new resize GetBufferSetLength
n)

char* string CString free

delete (delete[]) ReleaseBuffer

ReleaseBufferSetLength

( ) >

CString > string > char*;

( )

CString > string >char*;

( )

char* = string > CString
        ,       ? 3     6  ,    CString  char * ,         ,      .       

C/C++ code

   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> string CString   CString.Format( " %s " , string .c_str()); c_str() data()      char CString   CString.Format( " %s " , char * );       char string    string s( char * );        CString string    string s(CString.GetBuffer());    string char *    char * p = string .c_str(); CString char * CString.GetBuffer();