C++文字列間変換-Unicode文字セット
10893 ワード
この文書では、Unicode文字セットの下の文字列間および文字列と数値間の変換について説明します.1.string、char*およびconst char*<1>string->char*
<2>string->const char*
<3>char*->string
<4>char*->const char*
<5>const char*->string
<6>const char*->char*
2.char*stringとCString
<1>char*->CString
<2>string->CString
<3>CString->char*
<4>CString->string
3.数字と文字列の変換1.doubleとstring
<2>string->double
2.doubleとchar*<1>double->char*
<2>char*->double
3.doubleとCString<1>double->CString
<2>CString->double
4その他の文字列間の変換<1>LPCWSTR回転char*
<2>string回転LPWSTRTまたはWCHAR
char *ctr = new char[str.length()+1];
strcpy(ctr,str.c_str());
delete[]ctr; //
<2>string->const char*
string str("good");
const char *p = str.c_str();// const char*
<3>char*->string
char *ctr = "good";
string str(ctr);
//
string str2 = ctr;
<4>char*->const char*
char *ctr = "good";
const char *ctr2 = ctr;
<5>const char*->string
const char *ctr = "good";
string str(ctr);
<6>const char*->char*
const char *ctr = "good";
char *ctr2= new char[strlen(ctr)+1] ;
strcpy(ctr2, ctr);
delete[]ctr; //
// char[] char * ,
2.char*stringとCString
<1>char*->CString
char *p = "good";
CString cstr = p;
//
char *p = "good";
CString cstr;
cstr.Format("%s",p);
<2>string->CString
string str = "good";
CString cstr = str.c_str();
//
string str = "good";
CString cstr;
cstr.Format("%s",str.c_str());
<3>CString->char*
CString str = _T("good");
char *chr=new char[cstr.GetLength()+1];
WideCharToMultiByte(CP_ACP,0,str.AllocSysString(),-1,chr,cstr.GetLength()+1,NULL,NULL);
//
char *p;
int nCharLen;
nCharLen = WideCharToMultiByte(CP_ACP, 0,str.AllocSysString(), -1, NULL, 0, NULL, NULL);
p = new char [nCharLen + 1];
memset(p,0,nCharLen + 1);
WideCharToMultiByte(CP_ACP, 0, str.AllocSysString(), -1, p, nCharLen + 1, NULL, NULL);
<4>CString->string
CString cstr = "good";
string str = cstr;
//
string str = CStringA(cstr);
//
char *chr=new char[cstr.GetLength()+1];
WideCharToMultiByte(CP_ACP,0,cstr.AllocSysString(),-1,chr,cstr.GetLength()+1,NULL,NULL);
string str=chr;
3.数字と文字列の変換1.doubleとstring
<1>double->string
char temp[20];
double a = 0.631;
sprintf_s(temp, "%.2f", a);// 2
string str =temp;
<2>string->double
string a = "0.631";
double b = atof(a.c_str());
2.doubleとchar*<1>double->char*
char temp[20];
double a = 0.631;
sprintf_s(temp, "%.2f", a);// 2
char *p = temp;
<2>char*->double
char *a = "0.631";
double b = atof(a);
// int , “%f”->"%d"、atof->atoi
3.doubleとCString<1>double->CString
double a = 0.631;
CString cstr;
cstr.Format("%.2f",a);
<2>CString->double
CString cstr = "0.631";
double a = atof(cstr);
// int , “%f”->"%d"、atof->atoi
4その他の文字列間の変換<1>LPCWSTR回転char*
/***********************************************************************
:ConvertLPWSTRToChar
: ,LPCWSTR char*
:
lpwszStrIn :LPCWSTR
:
:pzh
:2016.3.4
:
***********************************************************************/
char* ConvertLPWSTRToChar (LPWSTR lpwszStrIn)
{
LPSTR pszOut = NULL;
if (lpwszStrIn != NULL)
{
int nInputStrLen = wcslen (lpwszStrIn);
// Double NULL Termination
int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char [nOutputStrLen];
if (pszOut)
{
memset (pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
}
return pszOut;
}
<2>string回転LPWSTRTまたはWCHAR
string strTemp = "113";
WCHAR wcharTemp[256]; //string LPWSTRT
MultiByteToWideChar(CP_ACP,0,strTemp.c_str(),-1,wcharTemp,sizeof(wcharTemp)/sizeof(wcharTemp[0]));