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