使用C++转换URL汉字编码~

之前搜到的CGI程序里URL的解码,好像很多使用的是Perl 或者 VB,现在不使用Chr()函数

之前写了个URL汉字编码对应字典,加载到<map>里,效率太低了

但是没想到机器内码也是一样的,所以直接调机器内码对应起来就行了~

使用机器内码直接解码就行了;

和对应码表比较,果然效率高了好多;

把这个发一下:

const string CUrlDecode::hex = "0123456789ABCDEF";

char CUrlDecode::ToIntNumber(char value)  
{
         return (char)(hex.find(toupper(value)));

}

string CUrlDecode::DecodeToChn(string word)    //Change the String To China Charactor
{
 char tempWord[4];
 string chn;

 tempWord[0] = ToIntNumber(word[0]);      //make the B to 11 -- 00001011
 tempWord[1] = ToIntNumber(word[1]);      //make the 0 to 0  -- 00000000
 tempWord[2] = ToIntNumber(word[2]);      //make the A to 10 -- 00001010
 tempWord[3] = ToIntNumber(word[3]);      //make the 1 to 1  -- 00000001

 chn.resize(2);
 chn[0] = (tempWord[0] << 4) | tempWord[1];    //to change the BO to 10110000
 chn[1] = (tempWord[2] << 4) | tempWord[3];    //to change the A1 to 10100001

 return chn;        //Now the chnWord is 10110000,10100001:十六位编码-“啊” 
}

这里使用汉字“啊”,做例子,使用的时候,由于URL里编码为%B0%A1

使string word得到B0A1(可以使用getenv函数,再使用字符串加以处理)

然后调用DecodeToChn(word)就可以了,返回的即为“啊”这个汉字~

 

好像c++7.1也有写.net时候的问题,今天定义的string chn;

写chn的时候居然感知不出来,return后也丢失了chn这个变量,不得不把这个函数删了从头写一遍~~

 

不过做的URL汉字对应的码表也算使一种资源或参考吧~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值