C-urldecode function Compilation

Source: Internet
Author: User
Tags define null null null

On the other hand, the interviewer asked me to write this function on paper. The principle of this function is actually very simple.
For example, % 20 is actually the corresponding ascii 32 character (Space.
During the interview, I only wrote half of it, which was too time-consuming. Now I am back to use my computer to write it.
We can draw a state conversion graph for this type of string parsing function, that is, the graph in the compilation principle.
Since this function is relatively simple, I just need to draw a flowchart.

The figure is drawn in the libreOffice suite draw under ubuntu. It is so inconvenient. It is not convenient to use Dia. It's better to use visio.
The flowchart is available, and the next step is the function code segment.

1/* 2 * main. c 3*4 * Created on: 2012-4-23 5 * Author: huangjacky 6 */7 8 # include <stdio. h> 9 # include <string. h> 10 # include <stdlib. h> 11 12 # define null NULL; 13 14 int hexchar2int (char ); 15 16/** 17 * perform url Decoding on src 18 * param src char * urlencode after the string format 19 * return null: the string src format is incorrect, otherwise, the parsed string 20 */21 char * urldecode (char * src) {22 int len = strlen (src); 23 if (len % 3) // The number of digits is not 24. return NULL; 25 int cou Nt = len/3; 26 char * dst = (char *) malloc (sizeof (char) * (count + 1); 27 if (! Dst) // failed to allocate space 28 return null; 29 // save space, directly use the variables len and count to act as temporary variables 30 int flag = 1; 31 char * dst1 = dst; 32 while (* src) {// the string is not over 33 if (* src = '%') {// enter the resolution status 34 src ++; 35 len = hexchar2int (* src); 36 src ++; 37 count = hexchar2int (* src); 38 if (count =-1 | len =-1) {// determine whether the converted integer is valid 39 flag = 0; 40 break; 41} 42 * dst1 ++ = (char) (len <4) + count); // The string 43} else {44 flag = 0; 45 break; 46} 47 src ++; 48} 49 if (! Flag) {// if an error occurs during parsing 50 free (dst); 51 return null; 52} 53 * dst1 = 0; // Add \ 054 return dst; 55} 56/** 57 * at the end of the string to convert the hex character to the corresponding integer 58 * return 0 ~ 15: the conversion is successful.-1 indicates that c is not hexchar59 */60 int hexchar2int (char c) {61 if (c> = '0' & c <= '9 ') 62 return c-'0'; 63 else if (c> = 'A' & c <= 'F') 64 return c-'A' + 10; 65 else if (c> = 'A' & c <= 'F') 66 return c-'A' + 10; 67 else68 return-1; 69} 70 71 int main () {72 char * s = "% 48% 4d"; 73 printf ("s is: % s \ r \ n", s ); 74 char * d = urldecode (s); 75 if (d) {76 printf ("decode is: % s \ r \ n", d); 77 free (d ); 78} 79 printf ("now s is: % s", s); 80 return 0; 81}

Of course, the real url Decoding function is not just to parse this % xx, but to simply write a function to parse % xx.
I am HuangJacky, mainly engaged in technical exchanges.

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.