url Encode和Decode的實現
源碼(vs2005版本):http://download.csdn.net/detail/hherima/5108862
也可以下載源碼(ios版本):http://download.csdn.net/detail/hherima/5108862
下面是IOS的源碼,注意將源檔案尾碼改為mm相關的VC尾碼也改為mm
標頭檔
using namespace std;#include <string.h>#include <iostream>class UrlEncode{public: NSString* Encode(NSString*src); NSString* Decode(NSString* src);private: string HttpUrlDecode(const string& srcUrl); string HttpUrlEncode(const string& srcUrl); int hexchar2int(char c); string UrlEncodeFormat(const unsigned char cValue); string DecimalToHexString(unsigned int nValue);};
源檔案
#include "UrlEncode.h"NSString* UrlEncode::Encode(NSString*src){ string buf = HttpUrlEncode([src UTF8String]); return [NSString stringWithUTF8String:buf.c_str()];}//NSString* UrlEncode::Decode(NSString* src){ string buf = HttpUrlDecode([src UTF8String]); return [NSString stringWithUTF8String:buf.c_str()];}//string UrlEncode::HttpUrlDecode(const string& srcUrl){ string desStr;int length = 0;int flag =1;unsigned char firstNum = 0;unsigned char SecondNum = 0;const char * pchar = srcUrl.c_str();//.Ptr();while(length < srcUrl.length()){if(pchar[length]=='%')//&& length < srcUrl.length()-2{//最後一位,need break;if(length == srcUrl.length()-1){desStr +=pchar[length];//.append(pchar[length]);break;}//正常移位length++;firstNum = hexchar2int(pchar[length]);length++;SecondNum = hexchar2int(pchar[length]); if (firstNum == -1 || SecondNum == -1)//判斷字元轉換成的整數是否有效 { flag = 0; break; }desStr += char(firstNum<<4) + char(SecondNum);//.append(char(firstNum<<4) + char(SecondNum));}else if(pchar[length]=='+'){desStr +=' ';//.append(' ');//spaceb}//不是特殊字元的urlcode了,即英文字元,直接append到string即可else{desStr +=pchar[length];// .append(pchar[length]);}length ++;}if(flag == 0){return srcUrl;}return desStr;}//string UrlEncode::HttpUrlEncode(const string& srcUrl){ string encodedUrl=""; int length = srcUrl.length();if (length == 0){return encodedUrl;}string sDontEncode = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_~.";// // Parse a the chars in the urlfor (int i=0; i<length; i++){ char cToFind = srcUrl.at(i); if (-1 == sDontEncode.find(cToFind,0)){// Char not found encode it.string tmp = UrlEncodeFormat(cToFind);encodedUrl.append(tmp);}else if(cToFind == ' '){encodedUrl += '+';//.append('+');}else{encodedUrl += cToFind;//.append(&cToFind,1);}}return encodedUrl; }//string UrlEncode::UrlEncodeFormat(const unsigned char cValue){ string tmp;tmp.append("%");unsigned int nDiv = cValue/16;unsigned int nMod = cValue%16;tmp.append(DecimalToHexString(nDiv));tmp.append(DecimalToHexString(nMod));return tmp;}//string UrlEncode::DecimalToHexString(unsigned int nValue){ string tmp;if(nValue<10)tmp+= ((char)nValue +48);//append((int)nValue);else{switch(nValue){ case 10:tmp.append("A");break; case 11:tmp.append("B");break; case 12:tmp.append("C");break; case 13:tmp.append("D");break; case 14:tmp.append("E");break; case 15:tmp.append("F");break; default:break;}}return tmp;}/** *將hex字元轉換成對應的整數 *return 0~15:轉換成功,-1:表示c 不是 hexchar */int UrlEncode::hexchar2int(char c){ if (c >= '0' && c <= '9') return c - '0'; else if (c >= 'a' && c <= 'f') return c - 'a' + 10; else if (c >= 'A' && c <= 'F') return c - 'A' + 10; else return -1;}