網路傳輸文本,urlEncode和decode的iOS實現。

來源:互聯網
上載者:User
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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.