如何在MFC中实现 全角字符转换成半角字符
1个回答
2016-11-20
展开全部
1.使用API函数LCMapString实现。
char * pshort = new char[512] ;
char * plong = new char[512] ;
memset( pshort , 0 , 512 ) ;
memset( plong , 0 , 512 ) ;
plong = "helloworld";
DWORD lcid = LANG_SYSTEM_DEFAULT ;
LCMapString( lcid ,LCMAP_HALFWIDTH , plong , 20 ,pshort , 10 ) ;
pshort则为半角字符,如果将参数换位LCMAP_FULLWIDTH,可将全角字符转换为半角字符。
2.使用计算
对于ascii码在0x20到0x7d之间的,也就是键盘上正常的字符(除了~之外)都有一个规律,比如空格的半角为0x20,而全角是0xa0a3,低位是固定的都是0xa3,即全角字符的低位上固定为0xa3,并且高位上固定大0x80;可以使用以下计算:
//全角转化为半角,输入字符中可以有半角,但是字符中的所有全角均认为是ASCII码所对应的全角字符
void FullToHalf(CString &strFull,CString &strHalf)
{
strHalf="";
const char *pszFull=strFull.GetBuffer(strFull.GetLength());
int npszFull=strlen(pszFull);
char* pszHalf=new char[npszFull+1];
memset(pszHalf,0,npszFull+1);
int iHalf=0,iFull=0;
for(int iFull=0;iFull<npszFull;)
{
if((BYTE)(pszFull[iFull])<128)//半角
{
memcpy(pszHalf+iHalf,pszFull+iFull,1);
iHalf++;
iFull++;
}
else//全角
{
BYTE bTemp=BYTE(pszFull[iFull+1]);
char cTemp=bTemp-128;
memcpy(pszHalf+iHalf,&cTemp,1);
iHalf++;
iFull++;
iFull++;
}
}
strHalf.Format("%s",pszHalf);
delete[] pszHalf;
}
//半角转化为全角,输入字符中可以有全角
void HalfToFull(CString &strHalf,CString &strFull)
{
strFull="";
const char *pszHalf=strHalf.GetBuffer(strHalf.GetLength());
int npszHalf=strlen(pszHalf);
char* pszFull=new char[npszHalf+npszHalf+1];
memset(pszFull,0,npszHalf+npszHalf+1);
int iHalf=0,iFull=0;
for(int iHalf=0;iHalf<npszHalf;)
{
if((BYTE)(pszHalf[iHalf])<128)//半角
{
BYTE bByte[2];
bByte[0]=160+3;
bByte[1]=128+(BYTE)pszHalf[iHalf];
memcpy(pszFull+iFull,bByte,2);
iHalf++;
iFull++;
iFull++;
}
else//全角
{
memcpy(pszFull+iFull,pszHalf+iHalf,2);
iHalf++;
iHalf++;
iFull++;
iFull++;
}
}
strFull.Format("%s",pszFull);
delete[] pszFull;
}
char * pshort = new char[512] ;
char * plong = new char[512] ;
memset( pshort , 0 , 512 ) ;
memset( plong , 0 , 512 ) ;
plong = "helloworld";
DWORD lcid = LANG_SYSTEM_DEFAULT ;
LCMapString( lcid ,LCMAP_HALFWIDTH , plong , 20 ,pshort , 10 ) ;
pshort则为半角字符,如果将参数换位LCMAP_FULLWIDTH,可将全角字符转换为半角字符。
2.使用计算
对于ascii码在0x20到0x7d之间的,也就是键盘上正常的字符(除了~之外)都有一个规律,比如空格的半角为0x20,而全角是0xa0a3,低位是固定的都是0xa3,即全角字符的低位上固定为0xa3,并且高位上固定大0x80;可以使用以下计算:
//全角转化为半角,输入字符中可以有半角,但是字符中的所有全角均认为是ASCII码所对应的全角字符
void FullToHalf(CString &strFull,CString &strHalf)
{
strHalf="";
const char *pszFull=strFull.GetBuffer(strFull.GetLength());
int npszFull=strlen(pszFull);
char* pszHalf=new char[npszFull+1];
memset(pszHalf,0,npszFull+1);
int iHalf=0,iFull=0;
for(int iFull=0;iFull<npszFull;)
{
if((BYTE)(pszFull[iFull])<128)//半角
{
memcpy(pszHalf+iHalf,pszFull+iFull,1);
iHalf++;
iFull++;
}
else//全角
{
BYTE bTemp=BYTE(pszFull[iFull+1]);
char cTemp=bTemp-128;
memcpy(pszHalf+iHalf,&cTemp,1);
iHalf++;
iFull++;
iFull++;
}
}
strHalf.Format("%s",pszHalf);
delete[] pszHalf;
}
//半角转化为全角,输入字符中可以有全角
void HalfToFull(CString &strHalf,CString &strFull)
{
strFull="";
const char *pszHalf=strHalf.GetBuffer(strHalf.GetLength());
int npszHalf=strlen(pszHalf);
char* pszFull=new char[npszHalf+npszHalf+1];
memset(pszFull,0,npszHalf+npszHalf+1);
int iHalf=0,iFull=0;
for(int iHalf=0;iHalf<npszHalf;)
{
if((BYTE)(pszHalf[iHalf])<128)//半角
{
BYTE bByte[2];
bByte[0]=160+3;
bByte[1]=128+(BYTE)pszHalf[iHalf];
memcpy(pszFull+iFull,bByte,2);
iHalf++;
iFull++;
iFull++;
}
else//全角
{
memcpy(pszFull+iFull,pszHalf+iHalf,2);
iHalf++;
iHalf++;
iFull++;
iFull++;
}
}
strFull.Format("%s",pszFull);
delete[] pszFull;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询