<script type="text/javascript"><!--
google_ad_client = "pub-0241434510974184";
/* auto-http.cn 右边 ,468x60 */
google_ad_slot = "0902256228";
google_ad_width = 468;
google_ad_height = 60;
// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
今天做程序因为要用到UrlEncode和UrlDecode,所以到网上找了一下,想用个现成的。找来找去,发现郑昀老师的代码思路是最清晰的,也很和胃口,但可惜的是只有一半 —— UrlEncode(),又看了看其他人写的,都不怎么样。所以一咬牙,干脆自己动手把UrlDecode()补上了,再做成头文件,以后用起来也方便(懒人啦)。
看见还有很多人搜索,所以贴出来一份给其他懒人。
代码如下:
inline BYTE toHex(const BYTE &x)
{
return x > 9 ? x + 55: x + 48;
}
inline BYTE toByte(const BYTE &x)
{
return x > 57? x - 55: x - 48;
}
CString URLDecode(CString sIn)
{
CString sOut;
const int nLen = sIn.GetLength() + 1;
register LPBYTE pOutTmp = NULL;
LPBYTE pOutBuf = NULL;
register LPBYTE pInTmp = NULL;
LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
//alloc out buffer
pOutBuf = (LPBYTE)sOut.GetBuffer(nLen);
if(pOutBuf)
{
pInTmp = pInBuf;
pOutTmp = pOutBuf;
// do encoding
while (*pInTmp)
{
if('%'==*pInTmp)
{
pInTmp++;
*pOutTmp++ = (toByte(*pInTmp)%16<<4) + toByte(*(pInTmp+1))%16;//高4位+低4位
pInTmp++;
}
else if('+'==*pInTmp)
*pOutTmp++ = ' ';
else
*pOutTmp++ = *pInTmp;
pInTmp++;
}
*pOutTmp = '/0';
sOut.ReleaseBuffer();
}
sIn.ReleaseBuffer();
return sOut;
}
CString URLEncode(CString sIn)
{
CString sOut;
const int nLen = sIn.GetLength() + 1;
register LPBYTE pOutTmp = NULL;
LPBYTE pOutBuf = NULL;
register LPBYTE pInTmp = NULL;
LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
//alloc out buffer
pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3);
if(pOutBuf)
{
pInTmp = pInBuf;
pOutTmp = pOutBuf;
// do encoding
while (*pInTmp)
{
if(isalnum(*pInTmp) || '-'==*pInTmp || '_'==*pInTmp || '.'==*pInTmp)
*pOutTmp++ = *pInTmp;
else if(isspace(*pInTmp))
*pOutTmp++ = '+';
else
{
*pOutTmp++ = '%';
*pOutTmp++ = toHex(*pInTmp>>4);//高4位
*pOutTmp++ = toHex(*pInTmp%16);//低4位
}
pInTmp++;
}
*pOutTmp = '/0';
sOut.ReleaseBuffer();
}
sIn.ReleaseBuffer();
return sOut;
}
代码中用了两个内联函数,想一想,那么放着也挺好,没必要整合进去。原源代码中仅对空格做了判断,Monyer又填上了“-_.”,算是更标准了些。
把这个函数放进去后,一个小工具的主要函数便完成了,再修改修改,明天放出来。
Monyer!
分享到:
相关推荐
urlEncode or urlDecode
主要介绍了ASP中实现的URLEncode、URLDecode自定义函数,和ASP自带的server.urlencode是不一样的哦,需要的朋友可以参考下
在ASP中传递参数时有一个很有用的系统函数Server.UrlEncode,可以将一些非字母数字的特殊符号转换成标准URL编码(其实就是16进制ASC码),这样就解决了参数传递问题,然后我以为也提供了Server.UrlDecode,但使用后...
urlencode urldecode实现,在xcode里使用c++混编
自己写的,可能有bug,请大家一块学习 环境为PB12 函数(及参数) 作用 arraysort 对一维数组进行排序 ...urldecode 将指定字符串以进行指定字符集url解码 urlencode 将指定字符串以进行指定字符集url编码 涨价了,哈哈
主要介绍了用JavaScript实现PHP的urlencode与urldecode函数,很多情况下我们用了出来php urlencode出来的网址,需要的朋友可以参考下
js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s) //注意编码 需要注意的几点: 1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。...
今天小编就为大家分享一篇Python3中urlencode和urldecode的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
UrlEncodeUrlDecodeUrl解码Url编码工具 基于UrlEncode,UrlDecode的解码编码工具,支持双向。
这个是 utf-8版本的 js实现 php的 urlencode() 和 urldecode()两个函数的功能。 在传送cookie的时候 在 php端实现 url编码 但要用 js来解码cookie的时候 就出现汉字不能不能识别的问题 这个 js 很好的解决
易语言URL编解码源码,URL编解码,URLEncode,URLDecode
本文用C++来实现UrlEncode!
举例:asp.net用Get方式传输的URL为:”WebPage.asp?str=”+HttpUtility.UrlEncode(str) ,解码方式为HttpUtility.UrlDecode(Request.QueryString[“str”].ToString().Trim()) asp的Get方式传送为”webPage.aspx?str=...
代码如下:‘================================================ ‘函数名:URLDecode ‘作 用:URL解码 ‘================================================ Function URLDecode(ByVal urlcode) Dim start,final,...
idea插件toolsMix (记录下班时间、实时中英文翻译、jsonView、urlEncode、urlDecode、timeFormate) 解压toolsMix.zip,toolsMix目录下的zip包为打包好的插件包,可以直接安装
主要介绍了php自定义urlencode,urldecode函数,实例分析了php字符串转码的技巧,具有一定参考借鉴价值,需要的朋友可以参考下