UTF8 URL的字符串转换

UTF8 URL的字符串转换



网页常常是UTF8的,而我们用VC用Wininet相关API  POST数据时,有中文的话,经常是%XX%XX%XX这种形式,这样的话,就能方便做出在线翻译之类的小软件,HOHO

所以写了个UTF8 URL编码转换函数

None.gifBOOL ConvertStringToURLCoding(CString &strDest, const char* strUTF8, int iLength)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    strDest.Empty();
InBlock.gif    CString strTemp;
InBlock.gif    
int i = 0;
InBlock.gif    
while(i < iLength)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ((unsigned)strUTF8[i] <= (unsigned char)0x7f)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ //字母和数字不转换
InBlock.gif
InBlock.gif            
if ((strUTF8[i] >= '0' && strUTF8[i] <= '9'|| 
InBlock.gif                (strUTF8[i] 
>= 'A' && strUTF8[i] <= 'Z'||
InBlock.gif                (strUTF8[i] >= 'a' && strUTF8[i] <= 'z'))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strDest 
+= (char)strUTF8[i];
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
else if (strUTF8[i] == ' ')    //空格转换成+号
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strDest 
+= '+';
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                strTemp.Format(
"%%%02X", (unsigned char)strUTF8[i]);    //其他标点符号
InBlock.gif                strDest 
+= strTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            i++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        else
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{        //汉字或者其他的uft8文字,每3个字节一转
InBlock.gif            strTemp.Format(
"%%%02X%%%02X%%%02X", (unsigned char)strUTF8[i], 
InBlock.gif                (unsigned 
char)strUTF8[i + 1], (unsigned char)strUTF8[i + 2]);
InBlock.gif            strDest 
+= strTemp;
InBlock.gif            i 
+= 3;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
InBlock.gif    if (i == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return FALSE;
ExpandedSubBlockEnd.gif    }

InBlock.gif    return TRUE;
ExpandedBlockEnd.gif}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值