⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wang.h

📁 实现了8种转换:包括Unicode<->gbk,gbk<->base 64,gbk<->hz这六种字符格式的相互转换.
💻 H
字号:
typedef short unsigned int USINT;
typedef unsigned char USCHAR;
int command()
{
    int choose;
	cout<<" 1-----------------------the list of unicode to gbk "<<endl;
	cout<<" 2------------------------the list of Gbk to unicode "<<endl;
	cout<<" 3-------------------------change a gbk file to unicode file" <<endl;
	cout<<" 4-------------------------change an uniode file to gbk file "<<endl;
	cout<<" 5-------------------------change a gbk file to base 64 file "<<endl;
	cout<<" 6-------------------------change a base64 file to gbk file "<<endl;
	cout<<" 7-------------------------change a gbk file to hz file "<<endl;
	cout<<" 8-------------------------change a hz file to gbk file "<<endl;
	cout<<" 9-------------------------quit"<<endl;
	cout<<" input the command :";
    cin>>choose;
	return choose;
}
bool UnToGb()
{
	ofstream outfile("D:\\unicodelist.txt",ios::binary);
	ofstream outfile_o("D:\\unicodetogbklist.txt",ios::binary);
	ofstream out_file("D:\\unidcodetogbkcode.txt",ios::binary);
	if(!outfile)
 		return  false ;
 	if(!out_file)
 		return  false;
 	if(!outfile_o)
  		return  false ;
 	USINT c=(USINT) 0xfffe;
	char a[3];
	outfile.write((char *)(&c),sizeof(USINT));
	a[2]='\0';
	USCHAR  l,m;
	USINT t;
	for(USCHAR i=(USCHAR)0x4E;i<(USCHAR)0x9F;i++)
		for(USCHAR j=(USCHAR)0x00;j<(USCHAR)0xFF;j++)
		{
			if(i==(USCHAR)0x9F&&j==(USCHAR)0xA5)
				break;
			c=j*(0xFF+1)+i;
			outfile.write((char *)(&c),sizeof(USINT));
			c=i*(0xFF+1)+j;
			outfile_o<<hex<<c<<"<=>";
			WideCharToMultiByte(CP_ACP,NULL,&c,-1,a,sizeof(WCHAR),NULL,NULL);
			out_file.write((char *)a,2);
            l=(USCHAR)a[0];
			m=(USCHAR)a[1];
			t=l*(0xFF+1)+m;
			outfile_o<<hex<<t<<" ";
		}
		outfile.close();
		out_file.close();
		outfile_o.close();
		return true;	
}
bool GbToUn()
{
	ofstream outfile("D:\\gbklist3.txt",ios::binary);
	ofstream out_file("D:\\gbklisttounicode.txt",ios::binary);
    ofstream outfile_o("D:\\gbkcodetounicode.txt",ios::binary);
	
	if(!outfile)
		return  false ;
	if(!out_file)
		return  false;
	if(!outfile_o)
		return  false ;
	USCHAR d[2];
	USINT c,b=0xfeff;
	WCHAR a;
    out_file.write((char *)(&b),sizeof(USINT));
    for(USCHAR gbkh=(USCHAR)0xB0;gbkh<=(USCHAR)0xF7;gbkh++)
		for(USCHAR gbkl=(USCHAR)0xA1;gbkl<=(USCHAR)0xFE;gbkl++)
		{
			c=gbkl*(0xff+1)+gbkh;
			outfile.write((char *)(&c),sizeof(USINT));
			outfile_o<<hex<<c<<"<=>";
			d[0]=gbkh;
			d[1]=gbkl;
            MultiByteToWideChar(CP_ACP,0,(char *)(&d),sizeof(d),&a,1);
			out_file.write((char *)(&a),sizeof(WCHAR));
			outfile_o<<hex<<a<<' ';
		}
		outfile.close();
		out_file.close();
		outfile_o.close();
		return true;
}
bool GbFToUnF()
{
	ifstream infile("D:\\gbk.txt",ios::binary);
	ofstream outfile("D:\\gbktounicode.txt",ios::binary);
	if(!infile)

		return  false ;

	if(!outfile)
 		return  false;
 	WCHAR a;
	int i=0;
	while(!infile.eof())
	{
		infile.read((char *)(&a),sizeof(WCHAR));
		i++;
	}
	WCHAR *b= new WCHAR [i-1];
	i=0;
	infile.clear();
	infile.seekg(0);
	while(!infile.eof())
	{
		infile.read((char *)(&b[i]),sizeof(WCHAR));
		i++;
	}
	USINT u=0xFEFF;
	outfile.write((char *)(&u),sizeof(USINT));
	USCHAR *c=new USCHAR[2*(i-1)];
	USINT j=0,temp,k=0;
	for(k=0;k<=i-1;k++,j=j+2)
	{
		temp=b[k];
		c[j+1]=temp/(0xff+1);
		temp=b[k];
		c[j]=(USCHAR)temp&0x00ff;
	}
	WCHAR *d=NULL;
	long num=MultiByteToWideChar(CP_ACP,0,(char *)c,2*(i-1),d,0);
	d=new WCHAR[2*num];
	MultiByteToWideChar(CP_ACP,0,(char *)c,2*(i-1),d,2*num);
	outfile.write((char *)d,2*num);
	delete [] b;
	delete [] c;
	delete [] d;
	return true;
}
//////////////////////
bool UFToGbF()
{
	ifstream infile("D:\\gbktounicode.txt",ios::binary);
	ofstream outfile("D:\\unicodetogbk.txt",ios::binary);
	if(!infile)
 		return  false ;
 	if(!outfile)
 		return  false;
 	WCHAR a;
	int i=0;
	infile.read((char *)(&a),sizeof(WCHAR));
	while(!infile.eof())
	{
		infile.read((char *)(&a),sizeof(WCHAR));
		i++;
	}
	WCHAR *b= new WCHAR [i-1];
	i=0;
	char *c=NULL;
	infile.clear();
	infile.seekg(0);
	infile.read((char *)(&a),sizeof(WCHAR));
	while(!infile.eof())
	{
		infile.read((char *)(&a),sizeof(WCHAR));
		b[i]=a;
		i++;
	}
	 UINT j;
     j=WideCharToMultiByte (936, 0, (PWSTR)b, i, NULL,0, NULL, NULL) ;
	 c=new char[j];
     WideCharToMultiByte (936, 0, (PWSTR) b, i, c,j, NULL, NULL) ;
     outfile.write((char *)c,j-1);
 return true;
}

///////////////////////////
char  Base2Chr( unsigned char n )
{
    n &= 0x3F;
    if ( n < 26 )
        return ( char )( n + 'A' );
    else if ( n < 52 )
        return ( char )( n - 26 + 'a' );
    else if ( n < 62 )
        return ( char )( n - 52 + '0' );
    else if ( n == 62 )
        return '+';
    else
        return '/';
}
//---------------------------------------------------------------------------
unsigned char Chr2Base( char c )
{
    if ( c >= 'A' && c <= 'Z' )
        return ( unsigned char )( c - 'A' );
    else if ( c >= 'a' && c <= 'z' )
        return ( unsigned char )( c - 'a' + 26 );
    else if ( c >= '0' && c <= '9' )
        return ( unsigned char )( c - '0' + 52 );
    else if ( c == '+' )
        return 62;
    else if ( c == '/' )
        return 63;
    else
        return 64;  //  无效字符
}
int Base64Encode( char * const aDest, const unsigned char * aSrc, int aLen )
{
    char        * p = aDest;
    int           i;
    unsigned char t;
	
    for ( i = 0; i < aLen; i++ )
    {
        switch ( i % 3 )
        {
        case 0 :
            *p++ = Base2Chr( *aSrc >> 2 );
            t = ( *aSrc++ << 4 ) & 0x3F;
            break;
        case 1 :
            *p++ = Base2Chr( t | ( *aSrc >> 4 ) );
            t = ( *aSrc++ << 2 ) & 0x3F;
            break;
        case 2 :
            *p++ = Base2Chr( t | ( *aSrc >> 6 ) );
            *p++ = Base2Chr( *aSrc++ );
            break;
        }
    }
    if ( aLen % 3 != 0 )
    {
        *p++ = Base2Chr( t );
        if ( aLen % 3 == 1 )
            *p++ = '=';
        *p++ = '=';
    }
    *p = 0;  
    return ( p - aDest );  
}

int Base64Decode( unsigned char * const aDest, const char * aSrc )
{
    unsigned char * p = aDest;
    int             i, n = strlen( aSrc );
    unsigned char   c, t;
	
    for ( i = 0; i < n; i++ )
    {
        if ( *aSrc == '=' )
            break;
        do {
			if ( *aSrc )
				c = Chr2Base( *aSrc++ );
            else
				c = 65;  //  字符串结束
        } while ( c == 64 );  //  跳过无效字符,如回车等
        if ( c == 65 )
			break;
        switch ( i % 4 )
        {
        case 0 :
            t = c << 2;
            break;
        case 1 :
            *p++ = ( unsigned char )( t | ( c >> 4 ) );
            t = ( unsigned char )( c << 4 );
            break;
        case 2 :
            *p++ = ( unsigned char )( t | ( c >> 2 ) );
            t = ( unsigned char )( c << 6 );
            break;
        case 3 :
            *p++ = ( unsigned char )( t | c );
            break;
        }
    }
    return ( p - aDest );
}
void base64_convert(int op)
//op为1时是Base64转Gbk
//op为0时是Gbk转Base64
{
	
	char temp,temp1;
	
	int k=0;
	int i=0;
	int l=0;
	char dest[5000],src[5000] ;
	if(op)
	{
		ifstream fin("D:\\gbktobase64.txt", ios::binary); 
		ofstream fout("D:\\base64togbk.txt", ios::binary);
		int m=0;
		while(!fin.eof())
		{
			fin.read((char *)(&temp), 1);
			
			if( temp == 0x0D ){
				fin.read((char *)(&temp), 1);
				fin.read((char *)(&temp), 1);
				if(temp1=='='||m<=75)
				{
					k=Base64Decode((unsigned char *)dest,src);
					for(int j=0;j<=k-2;j++)
						fout.write((char *)(&dest[j]), 1); 
					i=0;j=0;
					short ttt=0x0a0d;
					fout.write((char *)(&ttt), 2); 
					m=0;
					for(int u=0;u<5000;u++){
						src[u]=0;
						dest[u]=0;
					}
				}
			}
			
			src[i] = temp;
			i++;m++;
			temp1=temp;
		}
	}
	else
	{
		ifstream fin("D:\\gbk.txt", ios::binary); 
		ofstream fout("D:\\gbktobase64.txt", ios::binary);
		int j=0;
		while(!fin.eof())
		{
			fin.read((char *)(&temp), 1);
			if( temp == 0x0D ){
				fin.read((char *)(&temp), 1);
				fin.read((char *)(&temp), 1);
				k=Base64Encode(dest,(unsigned char *)src,i+1);
				int j;
				
				for(j=0;j<=k-1;j++){
					fout.write((char *)(&dest[j]), 1); 
					if(l==75){
						short ttt=0x0a0d;
						fout.write((char *)(&ttt), 2); 
					}
					l++;
				}
				l=0;
				short ttt=0x0a0d;
				fout.write((char *)(&ttt), 2); 
				i=0;
			}
			src[i]=temp;
			i++;
			
		}
		
	}
}
///////////////////////
//op为1时是Hz转Gbk
//op为0时是Gbk转Hz
void hz_convert(int op)
{
	char temp,temp1;
	bool bBegin=false;
	if(op)
	{
		ifstream fin("D:\\gbktohz.txt", ios::binary); 
		ofstream fout("D:\\hztogbk.txt", ios::binary); 
		while(!fin.eof())
		{
			fin.read((char *)(&temp), 1);
			
			if( temp == 0x0D ){
				fin.read((char *)(&temp), 1);
				fin.read((char *)(&temp), 1);
				short ttt=0x0a0d;
				fout.write((char *)(&ttt), 2); 
				if(fin.eof())
					return;
			}
			
			if(temp == '~')
			{
				temp1=temp;
				fin.read((char *)(&temp), 1);
				if(temp=='{'){
					bBegin=true;
					fin.read((char *)(&temp), 1);
					if(fin.eof())
						return;
				}
				else if(temp=='}'){
					bBegin=false;
					fin.read((char *)(&temp), 1);
					if(fin.eof())
						return;
				}
				else{
					
					if(bBegin){
						temp1=temp1 | 0x80;
						fout.write((char *)(&temp1), 1); 
					}
					else{
						fout.write((char *)(&temp1), 1); 
					}
				}
			}
			if(bBegin){
				temp=temp | 0x80;
				fout.write((char *)(&temp), 1); 
			}
			else{
				fout.write((char *)(&temp), 1); 
			}
		}
	}
	else
	{
	    ifstream fin("D:\\gbk.txt", ios::binary); 
		ofstream fout("D:\\gbktohz.txt", ios::binary); 
		while(!fin.eof())
		{
			fin.read((char *)(&temp), 1);
			
			if( temp == 0x0D ){
				fin.read((char *)(&temp), 1);
				fin.read((char *)(&temp), 1);
				short ttt=0x0a0d;
				fout.write((char *)(&ttt), 2); 
				if(fin.eof())
					return;
			}
			if( (temp & 0x80) == 0x80){
				if(!bBegin){
					char c1='~';
					char c2='{';
					fout.write((char *)(&c1), 1); 
					fout.write((char *)(&c2), 1); 
					bBegin=true;
				}
			}
			else{
				if(bBegin){
					char c1='~';
					char c2='}';
					fout.write((char *)(&c1), 1); 
					fout.write((char *)(&c2), 1); 
					bBegin=false;
				}
			}
			if(bBegin){
				temp=temp & 0x7f;
				fout.write((char *)(&temp), 1); 
			}
			else{
				fout.write((char *)(&temp), 1); 
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -