A c ++ class for URL encoding and decoding

Source: Internet
Author: User
# Pragma once # include <iostream> # include <string> # include <windows. h> using namespace STD; Class strcoding {public: strcoding (void );~ Strcoding (void); void utf_8togb2312 (string & pout, char * ptext, int Plen); // convert utf_8 to gb2312 void gb2312toutf_8 (string & pout, char * ptext, int Plen ); // convert gb2312 to utf_8 string url2312 (char * Str); // url2312 encoding string urlutf8 (char * Str); // urlutf8 encoding string urlutf8decode (string Str ); // urlutf8 decoding string url2312decode (string Str); // url2312 decoding PRIVATE: void gb2312tounicode (wchar * pout, char * gbbuffer); V Oid utf_8tounicode (wchar * pout, char * ptext); void unicodetoutf_8 (char * pout, wchar * ptext); void unicodetogb2312 (char * pout, wchar udata ); char chartoint (char ch); char strtobin (char * Str) ;};# include "stdafx. H "# include ". \ urlcodeing. H "// This is a class strcoding (strcoding. CPP file) strcoding: strcoding (void) {} strcoding ::~ Strcoding (void) {} void strcoding: gb2312tounicode (wchar * pout, char * gbbuffer) {: multibytetowidechar (cp_acp, mb_precomposed, gbbuffer, 2, pout, 1 ); return;} void strcoding: utf_8tounicode (wchar * pout, char * ptext) {char * uchar = (char *) pout; uchar [1] = (ptext [0] & 0x0f) <4) + (ptext [1]> 2) & 0x0f ); uchar [0] = (ptext [1] & 0x03) <6) + (ptext [2] & 0x3f); return;} void strcoding :: unicodetoutf_8 (char * pout, wchar * ptext) {// pay attention to the order of the wchar high and low characters. The low bytes are in front, and the high byte is in the back char * pchar = (char *) ptext; pout [0] = (0xe0 | (pchar [1] & 0xf0)> 4 )); pout [1] = (0x80 | (pchar [1] & 0x0f) <2) + (pchar [0] & 0xc0)> 6 ); pout [2] = (0x80 | (pchar [0] & 0x3f); return;} void strcoding: unicodetogb2312 (char * pout, wchar udata) {widechartomultibyte (cp_acp, null, & udata, 1, pout, sizeof (wchar), null, null); Return ;}// use char strcoding for URL Decoding :: chartoint (char ch) {If (CH> = '0' & Ch <= '9') Return (char) (CH-'0 '); if (CH> = 'A' & Ch <= 'F') Return (char) (CH-'A' + 10 ); if (CH> = 'A' & Ch <= 'F') Return (char) (CH-'A' + 10); Return-1;} Char strcoding :: strtobin (char * Str) {char tempword [2]; char CHN; tempword [0] = chartoint (STR [0]); // make the B to 11 -- 00001011 tempword [1] = chartoint (STR [1]); // make the 0 to 0 -- 00000000 CHN = (tempword [0] <4) | tempword [1]; // to change the Bo to 10110000 return CHN ;} // convert utf_8 to gb2312void strcoding: utf_8togb2312 (string & pout, char * ptext, int Plen) {char Buf [4]; char * rst = new char [Plen + (Plen> 2) + 2]; memset (BUF, 0, 4); memset (RST, 0, Plen + (Plen> 2) + 2); int I = 0; Int J = 0; while (I <Plen) {If (* (ptext + I)> = 0) {rst [J ++] = ptext [I ++];} else {wchar wtemp; utf_8tounicode (& wtemp, ptext + I); unicodetogb2312 (BUF, wtemp ); unsigned short int TMP = 0; TMP = rst [J] = Buf [0]; TMP = rst [J + 1] = Buf [1]; TMP = rst [J + 2] = Buf [2]; // newbuf [J] = ctemp [0]; // newbuf [J + 1] = ctemp [1]; I + = 3; j + = 2 ;}} rst [J] = '\ 0'; pout = RST; Delete [] RST;} // convert gb2312 to UTF-8void strcoding :: gb2312toutf_8 (string & pout, char * ptext, int Plen) {char Buf [4]; memset (BUF, 0, 4); pout. clear (); int I = 0; while (I <Plen) {// if it is an English copy, you can use if (ptext [I]> = 0) {char asciistr [2] = {0}; asciistr [0] = (ptext [I ++]); pout. append (asciistr);} else {wchar pbuffer; gb2312tounicode (& pbuffer, ptext + I); unicodetoutf_8 (BUF, & pbuffer); pout. append (BUF); I + = 2 ;}} return ;}// encode STR as the gb2312 URL encode in the webpage, which remains unchanged in English, double Bytes of Chinese characters such as % 3d % AE % 88 string strcoding: url2312 (char * Str) {string dd; size_t Len = strlen (STR); For (size_t I = 0; I <Len; I ++) {If (isalnum (byte) STR [I]) {char tempbuff [2]; sprintf (tempbuff, "% C ", STR [I]); DD. append (tempbuff);} else if (isspace (byte) STR [I]) {DD. append ("+");} else {char tempbuff [4]; sprintf (tempbuff, "% x", (byte *) Str) [I]> 4, (byte *) Str) [I] % 16); DD. append (tempbuff);} return dd;} // encode STR as the UTF-8 URL encode in the web page, which remains unchanged in English. Three bytes of Chinese characters, such as % 3d % AE % 88 string strcoding :: urlutf8 (char * Str) {string tt; string dd; gb2312toutf_8 (TT, STR, (INT) strlen (STR); size_t Len = TT. length (); For (size_t I = 0; I <Len; I ++) {If (isalnum (byte) TT. at (I) {char tempbuff [2] = {0}; sprintf (tempbuff, "% C", (byte) TT. at (I); DD. append (tempbuff);} else if (isspace (byte) TT. at (I) {DD. append ("+");} else {char tempbuff [4]; sprintf (tempbuff, "% x", (byte) TT. at (I)> 4, (byte) TT. at (I) % 16); DD. append (tempbuff) ;}} return dd ;}// decodes URL gb2312 from string strcoding: url2312decode (string Str) {string output = ""; char TMP [2]; int I = 0, idx = 0, ndx, Len = Str. length (); While (I <Len) {If (STR [I] = '%') {TMP [0] = STR [I + 1]; TMP [1] = STR [I + 2]; Output + = strtobin (TMP); I = I + 3;} else if (STR [I] = '+ ') {output + = ''; I ++;} else {output + = STR [I]; I ++ ;}} return output ;} // decodes URL utf8 from string strcoding: urlutf8decode (string Str) {string output = ""; string temp = url2312decode (STR); // utf_8togb2312 (output, (char *) temp. data (), strlen (temp. data (); Return output ;}

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.