This is a class strcoding (strCoding.h file)
#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);
};
This is a class strcoding (StrCoding.cpp file)
#include "StdAfx.h"
#include ". \strcoding.h"
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
}
//To use
Char strcoding:: Chartoint (char ch) {
if (ch>= ' 0 ') for solution URL && 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];
& nbsp; 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 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 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
}
to encode str as the GB2312 URL in the Web page encode, English invariant, Chinese character 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 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
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
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
String Strcoding::urlutf8decode (String str)
{
String output= "";
String temp =urlgb2312decode (str);//
utf_8togb2312 (output, (char *) temp.data (), strlen (Temp.data ()));
return output;
}
Test
#include "stdafx.h"
#include "StrCoding.h"
using namespace Std;
int main ()
{
Strcoding CFM;
String keyword= "Hello everyone, welcome you";
String temp= "";
String output= "";
UTF8 code that makes the keyword URL
temp= CFM. UrlUTF8 ((char *) keyword.data ());
cout<<temp<<endl;
Decoding the result of a URL's UTF8 code
Temp =cfm. Urlutf8decode (Temp);
cout<<temp<<endl;
gb2312 code that makes the keyword URL
Temp =cfm. UrlGB2312 ((char *) keyword.data ());
cout<<temp<<endl;
Decoding the result of a URL's gb2312 code
Temp =cfm. Urlgb2312decode (Temp);
cout<<temp<<endl;
Turn the key word GB2312 to Utf_8
cfm. Gb2312toutf_8 (Output, (char *) keyword.data (), strlen (Keyword.data ()));
cout<<output<<endl;
Convert GB2312 utf_8 to Chinese
cfm. utf_8togb2312 (Temp, (char *) output.data (), strlen (Output.data ()));
cout<<temp<<endl;
System ("Pasue");
GetChar ();
return 0;
//
}
Under Vc7win32 Debugging Pass