C + + codec URL

Source: Internet
Author: User
Tags sprintf

    #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),//utf_8 to gb2312 void Gb2312toutf_8 (string& pout,c Har *ptext, int plen);                           gb2312 to Utf_8 string UrlGB2312 (char * str);                             URLGB2312 encoded string UrlUTF8 (char * str);                  Urlutf8 encoded string Urlutf8decode (String str);                Urlutf8 decoding string Urlgb2312decode (string str);          urlgb2312 decoding private:void gb2312tounicode (wchar* pout,char *gbbuffer);          void 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) {//Note WCHAR The order of the high and low characters, the lower byte in front, and the higher byte in the rear                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 }//Do as solution URL using char strcoding:: 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 one--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;           }//utf_8 to gb2312 void 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]= ' + ';          POut = rst;      delete []rst;          }//gb2312 to UTF-8 void 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 in English direct copy can be 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 GB2312 URL in Web page encode, English unchanged, Chinese characters double byte such as%3d%ae%88 string strcoding::urlgb2312 (char * str) {          string DD;          size_t len = strlen (str); for (size_t i=0;i<len;i++) {if (Isalnum ((BYTE) str[i]) {char TEMPB                  UFF[2];                  sprintf (Tempbuff, "%c", Str[i]);              Dd.append (Tempbuff);              } else if (Isspace ((BYTE) str[i])) {dd.append ("+");                  } else {char tempbuff[4];                  sprintf (Tempbuff, "%%%x%x", ((byte*) str) [i] >>4, ((byte*) str) [i]%16);              Dd.append (Tempbuff);      }} return DD;           }//Encode str as UTF-8 URL in Web page encode, English unchanged, kanji three bytes 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 tem                  PBUFF[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%x", ((byte) tt.at (i)) >>4, ((byte) tt.at (i))%16);              Dd.append (Tempbuff);      }} return DD;              }//To decode the URL GB2312 string strcoding::urlgb2312decode (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;                }//To decode the URL UTF8 string strcoding::urlutf8decode (String str) {string output= "";                String temp =urlgb2312decode (str);//utf_8togb2312 (output, (char *) temp.data (), strlen (Temp.data ()));            return output;   }

C + + codec URL

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.