URL decoding (GBK/UTF8) C + + algorithm

Source: Internet
Author: User

The pages we visit will be encoded in different ways.

For example: "C + +" URL encoding (GBK) is "c%2b%2b"

So, how do you decode it?

By observing the encoding rules that can be encoded by URL

1, numbers and letters are not encoded.

2, the other symbol encoding method for the corresponding ASCII code of the 2-bit 16 binary front plus a "%".

For example, "+" table shows that the corresponding 16 binary number is 2B, so, "+" encoded into "%2b".

Program Ideas:

Define a char array to hold the data that needs to be decoded, and then iterate with the pointer, and if you hit the% number, transcode the two bits that follow it, then move the pointer back 3 bits. If you hit the numbers and letters, you can output them. Then the pointer moves back one bit.

    1. Program code:
 1 #include <iostream> 2//#include <stdio.h> 3 4 using namespace Std;     5 6 int Change (char *ch) 7 {8 int n = atoi (CH), 9 if (!n)//n = = 010 {One return (*ch-' A ' + 10); 12 }13 else if (n >= 1 && n <= 9) {[] return n;16}17}18 int main ()} {+ int c) Hange (char *); n char a[20] = {' a;25 '};23 int z;24 cin >> *p char a;26 = &L for (int i = 0; T 20; i++, p++) {if (int (*p) = = PNs), {+ char c[3] = "", or "char x[2] = {* (p+1) };32 char Y[2] = {* (p+2)};        x1 int = Change (x), y1 int = change (y), z = 16*x1 + y1;             $ printf ("%c", z), PNs p = p + 2;38}39 else if (*p! = ') ') 40 {41 cout << *p;42}43 if (*p = = ' + ') (break;46}47 48} Return 0;50}

The function to use:

    1. int atoi (const char *nptr) The atio function is a C language library function, a parameter nptr a string, if the first non-whitespace character exists, is a number or a sign to start a type conversion, and then detects a non-numeric (including The Terminator) character when the conversion is stopped, returning the integer number. Otherwise, zero is returned. Header file: #include <stdlib.h>, but in C + +, it seems to have included this library function.

Note the point:

1. Define array char a[20] Be sure to initialize the array to null. Otherwise the transcoding may have unexpected results. Because the content that you need to transcode can not fill a space, then the space data that is not occupied is unknown. So it's best to initialize to NULL when defining.

(1) Char str[10]= "";

(2) Char str[10]={' + '};

(3) Char str[10]; str[0]= ' + ';

The first (1) (2) way is to initialize all the elements of the STR array to ' + ', and the (3) method is to initialize only the initial element of the STR array to ' \ s '. If the size of the array is very large, then the first two methods will have a significant overhead. So, unless it is necessary (that is, we need to initialize all elements of the STR array to 0), we should use the first (3) way to initialize the string array.

2. Why use the change function. We know that ASCII code is able to directly add and subtract operations to get the corresponding 16 binary number. For example: "2B" = (' 2 '-' 0 ') * + (' B '-' A ' + 10) =43 If it is a numeric character, directly minus the character 0, if it is uppercase characters, directly minus the character A, lowercase similarly. The ASCII of B is 66,a, ' B '-' A ' = 1, then the corresponding should be 11. Is it possible to operate directly without using the change function? Here's another question. We cannot determine whether the first digit after the% number is a number or a letter, so it is not possible to calculate using a formula, which must first be judged by the change function. int n = atoi (CH); If it is a letter, the return value is returns (*ch-' A ' + 10); (only capitalization is considered here), and if it is a number, return n;

3, finally, determine whether the value of *p points to ' ", if it is, the end of the loop.

URL decoding (GBK/UTF8) C + + algorithm

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.