In the process of project development, c ++ url encoding and decoding are often used. This article will introduce the usage skills in detail. For more information, see
Copy codeThe Code is 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); // convert utf_8 to gb2312
Void GB2312ToUTF_8 (string & pOut, char * pText, int pLen); // convert gb2312 to utf_8
String UrlGB2312 (char * str); // The URL 2312 encoding.
String UrlUTF8 (char * str); // urlutf8 Encoding
String UrlUTF8Decode (string str); // urlutf8 Decoding
String urgb2312decode (string str); // URL 2312 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 codeThe Code is as follows: # include "StdAfx. h"
# Include "./urlcodeing. h"
// This Is A strCoding (strCoding. cpp file) class)
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 WCHAR high and low characters. The lower byte is in the front and the higher 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;
}
// Used for Url DecodingCopy codeThe Code is 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;
}
// Convert UTF_8 to gb2312Copy codeThe Code is 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 into UTF-8Copy codeThe Code is 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)
{
// Directly copy data in English
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. The English language remains unchanged. The double byte of Chinese characters is % 3D % AE % 88.Copy codeCode: 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 in the web page. The English language remains unchanged. The three bytes of Chinese characters are as follows: % 3D % AE % 88.Copy codeThe Code is 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", (BYTE) tt. at (I)> 4, (BYTE) tt. at (I) % 16 );
Dd. append (tempbuff );
}
}
Return dd;
}
// Decodes the url GB2312Copy codeCode: 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 the url utf8Copy codeThe Code is as follows: string strCoding: UrlUTF8Decode (string str)
{
String output = "";
String temp = url2312decode (str );//
UTF_8ToGB2312 (output, (char *) temp. data (), strlen (temp. data ()));
Return output;
}