Chinese decoding in CGI (C ++ language version)

xiaoxiao2021-03-06  136

The CGI program is a critical application that builds a web database application or a gateway program. Generally speaking, the CGI program does not directly deal with the IE browser, but is received or transmitted by the web server through environment variables, and the CGI program only dends with the environment variable of the web server. The browser often passes the information to the environment variable with the POST method. When you encounter special characters, such as Chinese characters, browsers don't send special characters to the web server, but first transform special characters' ASCII code into hexadecimal (such as Chinese characters "ordinary" sixteen System is:% C6% D5% CD% A8), is sent out. When reading the environment variable, the CGI program must not only decode the special character string, not only for special character strings. Most of the CGI books on the market are for the Perl language, but the programmers of CGI in the CGI, most difficult To the specific decoding process, I provide you with a complete decoding example. He compiled in VC , and it is intact (directly compiled and running the following example, enter "% C6% D5% CD% A8", will output "Ordinary" two words. A total of two files: main.cpp, main.h

Let's see main.cpp first

#include

#include

#include

#include "main.h"

Void main (void)

{

Char line [80];

Class decode_string unscape_str;

While (1)

{

Printf ("INPUT Your Code String or Exit

To Close: / N ");

Gets (line);

IF (! strcmp (line, "exit")) BREAK;

Printf ("This Decode String IS:% S / N", Unscape_Str.Translate_to_chinesse (line));

}

}

Looking at Main.h:

#include

#include

#include

Class decode_string {

Private:

PUBLIC:

Char * translate_to_chinesse (char *);

}

Char * decode_string :: Translate_to_chinessese

(Char * Dest_STR)

{register int x = 0, y = 0;

Char * TEMP_STR = NULL;

CHAR ASCII_ONE, ASCII_TWO;

Temp_str = (char *) Malloc (Strlen (DEST_STR) * SIZEOF (CHAR) 1);

While (temp_str [x])

{

IF ((Temp_STR [x] = dest_str [y]) == '%')

{IF (dest_str [y 1]> = 'a')

ASCII_ONE = (DEST_STR [Y 1] & 0xDF) - 'a') 10;

Else Ascii_one = DEST_STR [Y 1] - '0';

IF (DEST_STR [Y 2]> = 'a') ASCII_TWO = (DEST_STR [Y 2] & 0xDF) - 'A') 10; Else ASCII_TWO = DEST_STR [Y 2] -'0 ';

Temp_str [x] = ASCII_ONE * 16 ASCII_TWO; Y = 2;

} x ; y ;} temp_str [x] = '/ 0';

Return (TEMP_STR);

}

In the above example, only one member function char * decode_string :: translate_to_chinesse (char * dest_str) is a complete decoding function, like friends who use C language can add it to your program separately; friends like C You can add this class according to your needs.

转载请注明原文地址:https://www.9cbs.com/read-127306.html

New Post(0)