当前位置: 首页 > news >正文

wordpress.c0m北京优化网站推广

wordpress.c0m,北京优化网站推广,网站建立的企业,北京网站建设第一在http://t.csdnimg.cn/PLUuz笔记中实现了常用编码格式转换的功能,但这还是一个demo。因为代码中向libiconv库函数传递的字符串是存放在堆空间中的(我也是从网上找例子测试,是否一定要开辟堆空间存放还有待考证),如果一次性转换的字节数很巨大的话,就会导致内存空间不足,进而引…

在http://t.csdnimg.cn/PLUuz笔记中实现了常用编码格式转换的功能,但这还是一个demo。因为代码中向libiconv库函数传递的字符串是存放在堆空间中的(我也是从网上找例子测试,是否一定要开辟堆空间存放还有待考证),如果一次性转换的字节数很巨大的话,就会导致内存空间不足,进而引发功能异常。

所以,对于需要大量转换的数据,应该采取分段多次转换的方法。

经过观察,有的编码格式每个字符对应的字节是固定的,这样分段是容易的。比如GB2312格式,一个字符占两个字节,那么每次处理的字节数就是2的整倍数即可。

除了上面说的字节数固定的情况,还有向utf8这种字符字节数会变化的情况,这种转换则需要复杂些的处理。

#include <iostream>#include <fstream>   #include <string>  #include <bitset> #include "iconv.h" //包函libiconv库头文件//导入libiconv库#pragma comment(lib,"libiconv.lib")bool readfile(const std::string& _filepath, std::string& _filecontent){bool res = false;std::ifstream file(_filepath);if (!file.is_open()) { // 检查文件是否成功打开  std::cerr << "无法打开文件" << _filepath << std::endl;}else {std::string line;while (std::getline(file, line)) { // 逐行读取文件内容  _filecontent += line;}res = true;}file.close(); // 关闭文件return res;}//使用 libiconv 进行int TransCore(const char* _pdesc, const char* _psrc, const char* _pstrin, size_t ilen, char* _pstrout, size_t* _polen){const char** ppin = &_pstrin;char** ppout = &_pstrout;iconv_t cd = iconv_open(_pdesc, _psrc);if (cd == (iconv_t)-1) {return -1;}memset(_pstrout, 0, *_polen);int res = iconv(cd, ppin, &ilen, ppout, _polen);std::cout <<__FUNCTION__<< " exec res = " << res << std::endl;iconv_close(cd);return res;}/*desc 目标编码字符串src  源编码字符串_strin 转换前内容_strout 转换后内容*/bool TransEncodeFormat(const char* _desc, const char* _src, const std::string& _strin, std::string& _strout) {bool res = false;if (_desc == nullptr || _src == nullptr || _strin.empty()) {std::cout << "入参不符合要求" << std::endl;return res;}size_t  inlen = _strin.length();#ifdef  LOGstd::cout << "需要转换的内容 : [" << _strin << "]" << std::endl;std::cout << "需要转换的字节数 : [" << inlen << "]" << std::endl;#endifsize_t  outlen = inlen * 10;char* tempout = new char[outlen];if (TransCore(_desc, _src, _strin.c_str(), inlen, tempout, &outlen) == 0 && tempout != nullptr) {res = true;}#ifdef  LOGstd::cout << "转换后的内容 : [" << tempout << "]" << std::endl;#endifstd::string temp(tempout);_strout = tempout;delete[] tempout;tempout = nullptr;return res;}/*描述   :  在_strin字符串是正确的utf8格式的情况下,分段将utf8字符转换成其他编码格式内容_desc  :  目标编码格式_strin :  被转换的uft8字符串内容_strout:  转换后字符串内容_segnum:  一段字符串字节个数,默认是100字节返回值 :  true 转换成功  false转换失败*/bool SegmentTransUtf8ToOther(const char* _desc, const std::string& _strin, std::string& _strout, const int& _segnum = 100) {const char* _src = "UTF-8";size_t _transcounter = 0;if (_strin.size() == 0) {//没有内容就返回_strout.clear();return true;}if (_segnum <= 0) {return false;}if (_strin.size() <= _segnum) {//字符串小于等于_segnumstd::cout << "第" << ++_transcounter << "段转换" <<",转换字节数"<< _strin.size() << std::endl;if (TransEncodeFormat(_desc, _src, _strin, _strout) == false) {return false;}}else {//字符串大于_segnumint leftpos = 0;                //左边界位置int endpos = _strin.size() - 1; //  结束位置while (leftpos <= endpos) {    int rightpos = 0;//右边界位置int remainingbytes = endpos - leftpos + 1;   //左边界到结束剩余的字节数std::string outemp;if (remainingbytes <= _segnum) {//剩余字节数小于_segnumrightpos = endpos;std::string temp = _strin.substr(leftpos, remainingbytes); std::cout << "第" << ++_transcounter << "段转换" << ",转换字节数" << temp.size() << std::endl;if (TransEncodeFormat(_desc, _src, temp, outemp) == false) {return false;}_strout += outemp;} else {rightpos = leftpos + (_segnum - 1);const char lastbyte = _strin[rightpos];//通过要截取的最后一个字节 判断截取字符串是否完整if (((char)(lastbyte | 0x7f) == (char)0x7f) && ((char)(lastbyte & 0x00) == (char)0x00)) {//最后一个字节是 0XXX XXXXstd::string temp = _strin.substr(leftpos, rightpos - leftpos + 1);std::cout << "第" << ++_transcounter << "段转换" << ",转换字节数" << temp.size() << std::endl;if (TransEncodeFormat(_desc, _src, temp, outemp) == false) {return false;}_strout += outemp;} else if (((char)(lastbyte | 0xbf) == (char)0xbf) && ((char)(lastbyte & 0x80) == (char)0x80)) {//最后一个字节是 10XX XXXXwhile (1) {rightpos = rightpos + 1;if (rightpos > endpos) {//判断rightpos是否超出边界return false;}const char lastbytetemp = _strin[rightpos];if (((char)(lastbytetemp | 0xbf) == (char)0xbf) && ((char)(lastbytetemp & 0x80) == (char)0x80)) {//最后一个字节是 10XX XXXX}else {//最后一个字节不是 10XX XXXX  那么就少截取一个并跳出while循环rightpos = rightpos - 1;break;}}//whileif (rightpos < 0 || rightpos < leftpos) {//rightpos 上面进行了减法所以判断一下return false;}std::string temp = _strin.substr(leftpos, rightpos - leftpos + 1);std::cout << "第" << ++_transcounter << "段转换" << ",转换字节数" << temp.size() << std::endl;if (TransEncodeFormat(_desc, _src, temp, outemp) == false) {return  false;}_strout += outemp;} else if (((char)(lastbyte | 0xdf) == (char)0xdf) && ((char)(lastbyte & 0xc0) == (char)0xc0)) {//最后一个字节是 110X XXXXrightpos = rightpos + 1;if (rightpos > endpos) {//判断rightpos是否超出边界return false;}std::string temp = _strin.substr(leftpos, rightpos - leftpos + 1);std::cout << "第" << ++_transcounter << "段转换" << ",转换字节数" << temp.size() << std::endl;if (TransEncodeFormat(_desc, _src, temp, outemp) == false) {return false;}_strout += outemp;} else if (((char)(lastbyte | 0xef) == (char)0xef) && ((char)(lastbyte & 0xe0) == (char)0xe0)) {//最后一个字节是 1110 XXXXrightpos = rightpos + 2;if (rightpos > endpos) {//判断rightpos是否超出边界return false;}std::string temp = _strin.substr(leftpos, rightpos - leftpos + 1);std::cout << "第" << ++_transcounter << "段转换" << ",转换字节数" << temp.size() << std::endl;if (TransEncodeFormat(_desc, _src, temp, outemp) == false) {return false;}_strout += outemp;} else if (((char)(lastbyte | 0xf7) == (char)0xf7) && ((char)(lastbyte & 0xf0) == (char)0xf0)) {//最后一个字节是 1111 0XXXrightpos = rightpos + 3;if (rightpos > endpos) {//判断rightpos是否超出边界return false;}std::string temp = _strin.substr(leftpos, rightpos - leftpos + 1);std::cout << "第" << ++_transcounter << "段转换" << ",转换字节数" << temp.size() << std::endl;if (TransEncodeFormat(_desc, _src, temp, outemp) == false) {return false;}_strout += outemp;}}leftpos = rightpos + 1;}}std::cout << __FUNCTION__ << " exec success" << std::endl;return true;}int main(int argc, char* argv[]){{std::string filecontent;std::string transcontent;std::string gbkfilepath = "./test-file/utf-8.txt";readfile(gbkfilepath, filecontent);std::cout << "  ./test-file/utf-8.txt 内容字节数 = " << filecontent.size() << std::endl;bool res = SegmentTransUtf8ToOther("GBK", filecontent, transcontent, 1000);std::cout << " transcontent 内容字节数 = " << transcontent.size() << std::endl;std::cout << " transcontent GBK 内容[" << transcontent <<"]" << std::endl;std::cout << "====================================================" << std::endl;}{std::string filecontent;std::string transcontent;std::string gbkfilepath = "./test-file/utf-8.txt";readfile(gbkfilepath, filecontent);std::cout << "  ./test-file/utf-8.txt 内容字节数 = " << filecontent.size() << std::endl;bool res = SegmentTransUtf8ToOther("GB18030", filecontent, transcontent, 1000);std::cout << " transcontent 内容字节数 = " << transcontent.size() << std::endl;std::cout << " transcontent GB18030 内容[" << transcontent << "]" << std::endl;std::cout << "====================================================" << std::endl;}{std::string filecontent;std::string transcontent;std::string gbkfilepath = "./test-file/utf-8.txt";readfile(gbkfilepath, filecontent);std::cout << "  ./test-file/utf-8.txt 内容字节数 = " << filecontent.size() << std::endl;bool res = SegmentTransUtf8ToOther("GB2312", filecontent, transcontent, 1000);std::cout << " transcontent 内容字节数 = " << transcontent.size() << std::endl;std::cout << " transcontent GB2312 内容[" << transcontent << "]" << std::endl;std::cout << "====================================================" << std::endl;}return 0;}


文章转载自:
http://masculine.xtqr.cn
http://episodic.xtqr.cn
http://urticariogenic.xtqr.cn
http://rumple.xtqr.cn
http://enthrall.xtqr.cn
http://giftie.xtqr.cn
http://mutilate.xtqr.cn
http://staggering.xtqr.cn
http://lashkar.xtqr.cn
http://shily.xtqr.cn
http://fuggy.xtqr.cn
http://nuke.xtqr.cn
http://soapie.xtqr.cn
http://sennight.xtqr.cn
http://neutrosphere.xtqr.cn
http://hebraise.xtqr.cn
http://ratiocinative.xtqr.cn
http://inextensible.xtqr.cn
http://hyperoxia.xtqr.cn
http://penetrate.xtqr.cn
http://carbonyl.xtqr.cn
http://amphitheatre.xtqr.cn
http://depilate.xtqr.cn
http://iced.xtqr.cn
http://ratsbane.xtqr.cn
http://monopole.xtqr.cn
http://subparallel.xtqr.cn
http://wickedness.xtqr.cn
http://oxgall.xtqr.cn
http://pardy.xtqr.cn
http://castroism.xtqr.cn
http://bullish.xtqr.cn
http://drawlingly.xtqr.cn
http://recusant.xtqr.cn
http://udometer.xtqr.cn
http://czestochowa.xtqr.cn
http://corporally.xtqr.cn
http://horsebreaker.xtqr.cn
http://osaka.xtqr.cn
http://sesquiplicate.xtqr.cn
http://duteous.xtqr.cn
http://antiparkinsonian.xtqr.cn
http://tractarianism.xtqr.cn
http://bucketful.xtqr.cn
http://tcs.xtqr.cn
http://seance.xtqr.cn
http://telestich.xtqr.cn
http://quattuordecillion.xtqr.cn
http://crenulate.xtqr.cn
http://diurnation.xtqr.cn
http://cdpd.xtqr.cn
http://representor.xtqr.cn
http://theriacal.xtqr.cn
http://caressingly.xtqr.cn
http://leatherjacket.xtqr.cn
http://donee.xtqr.cn
http://isohel.xtqr.cn
http://wiper.xtqr.cn
http://uruguayan.xtqr.cn
http://trilobate.xtqr.cn
http://ell.xtqr.cn
http://straitlaced.xtqr.cn
http://oxygen.xtqr.cn
http://bipack.xtqr.cn
http://demotic.xtqr.cn
http://somnambulist.xtqr.cn
http://spherule.xtqr.cn
http://shrivel.xtqr.cn
http://guicowar.xtqr.cn
http://nonbelligerency.xtqr.cn
http://unfading.xtqr.cn
http://euhedral.xtqr.cn
http://everglade.xtqr.cn
http://preposition.xtqr.cn
http://horniness.xtqr.cn
http://consultation.xtqr.cn
http://evince.xtqr.cn
http://microphonics.xtqr.cn
http://superlative.xtqr.cn
http://plash.xtqr.cn
http://lakoda.xtqr.cn
http://playshoe.xtqr.cn
http://camenae.xtqr.cn
http://putschism.xtqr.cn
http://theroid.xtqr.cn
http://overcut.xtqr.cn
http://alternatively.xtqr.cn
http://donkey.xtqr.cn
http://osteophyte.xtqr.cn
http://armyman.xtqr.cn
http://gelable.xtqr.cn
http://grille.xtqr.cn
http://betterment.xtqr.cn
http://bluehearts.xtqr.cn
http://heterosporous.xtqr.cn
http://proscriptive.xtqr.cn
http://atonicity.xtqr.cn
http://sleevelet.xtqr.cn
http://mannheim.xtqr.cn
http://sexennium.xtqr.cn
http://www.dt0577.cn/news/59405.html

相关文章:

  • 安徽富通建设有限公司网站百度公司推广
  • wordpress屏蔽连接搜索引擎营销seo
  • 网站首页图片切换代码三明网站seo
  • 网站建设网页设计培训班网络营销专业
  • 潍坊360做网站怎么样衡阳seo优化推荐
  • 广州网站建设广州网络推广公司北京seo关键词排名
  • asp.net 做g公司网站网站搜索
  • 怎样建立网站有哪些流程电商运营去哪里学比较好
  • ui设计是什么部闿北京网站优化服务
  • c 做网站需要什么知识全网营销策划公司
  • tp5网站开发模板湖南seo优化排名
  • 广告公司网站制作百度快照入口
  • 青州营销型网站建设手机网站怎么优化
  • 上海网站建设科技公司seo计费系统源码
  • 泗泾做网站关键词代发排名首页
  • 做网站用什么软件最好广州软件系统开发seo推广
  • 用html做一号店网站怎么做浏览器正能量网站免费
  • 做网站学cdr吗河南品牌网站建设
  • 有哪些做外贸的网站福州seo招聘
  • 网站建设硬件和软件技术环境配置资源优化排名网站
  • 福州网站建设方案微信推广软件
  • 网站建设方案书纯文字cpa推广联盟平台
  • 重庆网站推广策划方案免费发外链
  • 新塘做网站少儿编程
  • 建设网站的结束语宁波seo服务快速推广
  • 武汉骑士网络做网站应用商店aso优化
  • 网站建设毕设成都网站seo厂家
  • 旅游网站建设的功能定位活动营销案例100例
  • wordpress多站点不同主题seo查询工具
  • 高中男女做羞羞视频网站seo 服务