`
hcmfys
  • 浏览: 348209 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

#ocx 格式转换

阅读更多

#include<iostream>
#include "iconv.h" 
using namespace std;
typedef unsigned char byte;



 class Convert {

	private:
	 int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen){
		iconv_t cd;
		int rc;
		char **pin = &inbuf;
		char **pout = &outbuf;

		cd = iconv_open(to_charset, from_charset);
		if (cd==0) return -1;
		memset(outbuf,0,outlen);
		if (iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen)==(size_t)-1) return -1;
		iconv_close(cd);
		return 0;
	}
	int hex2oct(char p)  {
		char  hexData[]  = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F' };
		int size=sizeof(hexData)/sizeof(char);
		for (int i = 0; i <size ; i++) {
			if ( hexData[i]  == p) {
				return i;
				}
			}
		return -1;
	}
/***
 * 
 * @param p1
 * @param p2
 * @param p3
 * @param p4
 * @param charset
 * @return
 *  
 */
	 string hexString2String(char p1, char p2, char p3, char p4, string& charset,string& toCharset){
		byte first = (byte) (hex2oct(p1) * 16 + hex2oct(p2));
		byte  second = ( byte) (hex2oct(p3) * 16 + hex2oct(p4));
		byte  bytes[3]  ;	 
		bytes[1] = first;
		bytes[0] = second;
		bytes[2]='\0';
		char out_buffer[16];
		char *from=const_cast<char*>(charset.c_str());
		char *to=const_cast<char*>(toCharset.c_str());
		size_t length=code_convert(from,to,(char*)bytes,3,out_buffer,16);
		if(length<0){
			cout<<"conver error \n";
		}
		 
		string ret="";
		ret=out_buffer;
	    return ret;

	}

	/**
	 * &#x7EC4;&#x7EC7;&#x4E13;&#x7EBF;&#x5F00;&#x901A;
	 * 
	 *  
	 */
  public:
	  string doDecoder(string& html, string& charset,string& toCharset){
		int size = html.size();
		string result = "";
		for (int i = 0; i < size; i++) {
			char c = html.at(i);
			if (c == '&') {
				if ((i + 7) < size) {
					if (html.at(i + 1) == '#' && ( html.at(i + 2) == 'X' || html.at(i + 2) == 'x')) {
						char p1 = html.at(i + 3);
						char p2 = html.at(i + 4);
						char p3 = html.at(i + 5);
						char p4 = html.at(i + 6);
						string s = hexString2String(p1, p2, p3, p4, charset,toCharset);
						result += s;
						i += 7;
					}
				} else {
					result += c;
					continue;
				}
			} else {
				result += c;
			}
		}
		return result;
	}


};


 int  main(int argv,char** argc)   {

		Convert c ;
		string s="&#x7EC4;&#x7EC7;&#x4E13;&#x7EBF;&#x5F00;&&#x901A;";
		string charset="utf-16";
		string toCharset="GB2312";
		string ret = c.doDecoder(s, charset ,toCharset);		 
		cout<<ret.c_str()<<endl;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics