The IOS implementation of network transmission text, urlencode, and decode.

Source: Internet
Author: User
Tags urlencode
Implementation of URL encode and decode

Source code (vs2005 version): http://download.csdn.net/detail/hherima/5108862

You can also download the source code (IOS version): http://download.csdn.net/detail/hherima/5108862

The following is the IOS source code. Change the source file suffix to mm-related VC suffix.

Header file

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);};

Source File

# Include "urlencode. H "nsstring * urlencode: encode (nsstring * SRC) {string Buf = httpurlencode ([SRC utf8string]); return [nsstring stringwithuf8string: Buf. c_str ()];} // nsstring * urlencode: Decode (nsstring * SRC) {string Buf = httpurldecode ([SRC utf8string]); return [nsstring stringwithuf8string: 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 {// the last digit, need break; If (length = srcurl. length ()-1) {desstr + = pchar [length]; //. append (pchar [length]); break;} // normal shift length ++; firstnum = hexchar2int (pchar [length]); length ++; secondnum = hexchar2int (pchar [length]); If (Firstnum =-1 | secondnum =-1) // checks whether the converted integer is valid {flag = 0; break ;} desstr + = char (firstnum <4) + char (secondnum );//. append (char (firstnum <4) + char (secondnum);} else if (pchar [length] = '+') {desstr + = '';//. append (''); // spaceb} // urlcode that is not a special character, that is, the English character. append directly to string to 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 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 unsign Ed 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"); br Eak; 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;}/*** converts the hex character to the corresponding integer * return 0 ~ 15: the conversion is successful.-1 indicates that C is not 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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.