C + + class URL encoding and decoding using tips _c language

Source: Internet
Author: User
In the project development process, often use to C + + URL encoding and decoding, this article will be detailed introduction to the use of skills, the need for friends can refer to the
Copy Code code as follows:

#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,char *ptext, int pLen); gb2312 Turn Utf_8
String UrlGB2312 (char * str); urlgb2312 Code
String UrlUTF8 (char * str); Urlutf8 Code
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);
};

Copy Code code as follows:

#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 The order of the WCHAR, the low byte in the front, the high byte 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
}

Used as a URL for the solution.
Copy Code code as follows:

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 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;
}

Utf_8 Turn gb2312
Copy Code code as follows:

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]= '/0 ';
pout = rst;
delete []rst;
}

GB2312 to UTF-8.
Copy Code code as follows:

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 you copy it directly in English, you can
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
}

to encode str as the GB2312 URL in the Web page encode, English invariant, Chinese character double byte such as%3d%ae%88
Copy Code code as follows:

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 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%x", ((byte*) str) [i] >>4, ((byte*) str) [i]%16);
Dd.append (Tempbuff);
}
}
return DD;
}

to encode str as the UTF-8 URL in the Web page encode, English unchanged, Chinese characters three bytes such as%3d%ae%88
Copy Code code as follows:

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%x", ((byte) tt.at (i)) >>4, ((byte) tt.at (i))%16);
Dd.append (Tempbuff);
}
}
return DD;
}

Decode the URL GB2312
Copy Code code as follows:

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;
}

Decode the URL utf8
Copy Code code as follows:

String Strcoding::urlutf8decode (String str)
{
String output= "";
String temp =urlgb2312decode (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.