The C language implementation of URL encode and URL decode, encodedecode

Source: Internet
Author: User
Tags url decode

The C language implementation of URL encode and URL decode, encodedecode

Reprinted from: http://blog.csdn.net/langeldep/article/details/6264058

The code in this article is modified from the PHP code, and only two functions are retained.

 

Int php_url_decode (char * str, int len );
Char * php_url_encode (char const * s, int len, int * new_length );

 

URL encoding is performed as follows:

 

Character "a"-"z", "A"-"Z", "0"-"9 ",". ","-"," * ", and" _ "are not encoded to maintain the original value;

Space "" is converted to the plus sign "+ ".

Each other byte is represented as a 3-character string in "% xy" format, encoded as a UTF-8.

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <ctype. h> # include <sys/types. h> static unsigned char hexchars [] = "0123456789 ABCDEF ";/**
* Convert a hexadecimal number to a hexadecimal number.
* For example, 0xE4 = 14*16 + 4 = 228
*/Static int php_htoi (char * s) {int value; int c; c = (unsigned char *) s) [0]; if (isupper (c )) c = tolower (c); value = (c> = '0' & c <= '9 '? C-'0': c-'A' + 10) * 16; c = (unsigned char *) s) [1]; if (isupper (c )) c = tolower (c); value + = c> = '0' & c <= '9 '? C-'0': c-'A' + 10; return (value);} char * php_url_encode (char const * s, int len, int * new_length) {register unsigned char c; unsigned char * to, * start; unsigned char const * from, * end; from = (unsigned char *) s; end = (unsigned char *) s + len; start = to = (unsigned char *) calloc (1, 3 * len + 1); while (from <end) {c = * from ++; if (c = '') {* to ++ = '+';} else if (c <'0' & c! = '-' & C! = '.') | (C <'A' & c> '9') | (c> 'Z' & c <'A' & c! = '_') | (C> 'Z') {to [0] = '%'; to [1] = hexchars [c> 4]; // convert binary to hexadecimal to [2] = hexchars [c & 15]; // convert binary to hexadecimal to + = 3;} else {* to ++ = c;} * to = 0; if (new_length) {* new_length = to-start;} return (char *) start;} int php_url_decode (char * str, int len) {char * dest = str; char * data = str; while (len --) {if (* data = '+') {* dest = '';} else if (* data = '%' & len> = 2 & isxdigit (Int) * (data + 1) & isxdigit (int) * (data + 2) {* dest = (char) php_htoi (data + 1 ); data + = 2; len-= 2;} else {* dest = * data;} data ++; dest ++;} * dest = '\ 0 '; return dest-str;} int main (int argc, char ** argv) {char * str = "Hello, world! ", * New_str; int len = 0, new_len = 0, old_len = 0; len = strlen (str); new_str = php_url_encode (str, len, & new_len ); printf ("new string: % s, new length: % d \ n", new_str, new_len); old_len = php_url_decode (new_str, new_len); printf ("old string: % s, old length: % d \ n ", new_str, old_len); return 0 ;}

Ps: I checked a lot of information to understand the urldecode principle. It turned out to be so simple.

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.