1.半角轉換為全角
CString CTestExcelChange091230Dlg::DbcToSbc(CString stringText)
{
CString str=L"";
int nLength=stringText.GetLength();
ASSERT(nLength!=0);
TCHAR *c=new TCHAR[sizeof(TCHAR)*nLength+1];
memset(c,0,sizeof(TCHAR)*nLength+1);
wcscpy(c,stringText);
for (int i=0;i<nLength;i++)
{
if (c[i]==32)
{
c[i]=(TCHAR)12288;
continue;
}
if (c[i]<127)
{
c[i]=(TCHAR)(c[i]+65248);
}
}
str.Format(L"%s",c);
delete c;
c=NULL;
return str;
}
2.全角轉換為半角
CString CSpss::SbcToDbc(CString stringText)
{
CString str=L"";
int nLength=stringText.GetLength();
ASSERT(nLength!=0);
TCHAR *c=new TCHAR[sizeof(TCHAR)*nLength+1];
memset(c,0,sizeof(TCHAR)*nLength+1);
wcscpy(c,stringText);
for (int i=0;i<nLength;i++)
{
if (c[i]==12288)
{ c[i]=(TCHAR)32;
continue;
}
if (c[i]>65280 && c[i]<65375)
c[i]=(TCHAR)(c[i]-65248);
}
str.Format(L"%s",c);
delete c;
c=NULL;
return str;
}
這段程序今天看來有許多不好的地方,現將第一個程序其改為如下:
void CTestDlg2008Dlg::DbcToSbc(const CString stringText,CString &str)
{
int nLength=stringText.GetLength();
ASSERT(nLength!=0);
TCHAR *c=new TCHAR[sizeof(TCHAR)*nLength+1];
memset(c,0,sizeof(TCHAR)*nLength+1);
wcscpy(c,stringText);
for (int i=0;i<nLength;i++)
{
if (c[i]==32)
{
c[i]=(TCHAR)12288;
continue;
}
if (c[i]<127)
{
c[i]=(TCHAR)(c[i]+65248);
}
}
str.Format(L"%s",c);
delete []c;
c=NULL;
}
2010.7.3