




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
多字节与UTF-8、Unicode之间的转换1. 2. 3. /多字节编码转为UTF8编码4. boolMBToUTF8(vector&pu8,constchar*pmb,int32mLen)5. 6. /convertanMBCSstringtowidechar7. int32nLen=MultiByteToWideChar(CP_ACP,0,pmb,mLen,NULL,0);8. 9. WCHAR*lpszW=NULL;10. try11. 12. lpszW=newWCHARnLen;13. 14. catch(bad_alloc&memExp)15. 16. returnfalse;17. 18. 19. int32nRtn=MultiByteToWideChar(CP_ACP,0,pmb,mLen,lpszW,nLen);20. 21. if(nRtn!=nLen)22. 23. deletelpszW;24. returnfalse;25. 26. /convertanwidecharstringtoutf827. int32utf8Len=WideCharToMultiByte(CP_UTF8,0,lpszW,nLen,NULL,0,NULL,NULL);28. if(utf8Len=0)29. 30. returnfalse;31. 32. pu8.resize(utf8Len);33. nRtn=WideCharToMultiByte(CP_UTF8,0,lpszW,nLen,&*pu8.begin(),utf8Len,NULL,NULL);34. deletelpszW;35. 36. if(nRtn!=utf8Len)37. 38. pu8.clear();39. returnfalse;40. 41. returntrue;42. 43. 44. /UTF8编码转为多字节编码45. boolUTF8ToMB(vector&pmb,constchar*pu8,int32utf8Len)46. 47. /convertanUTF8stringtowidechar48. int32nLen=MultiByteToWideChar(CP_UTF8,0,pu8,utf8Len,NULL,0);49. 50. WCHAR*lpszW=NULL;51. try52. 53. lpszW=newWCHARnLen;54. 55. catch(bad_alloc&memExp)56. 57. returnfalse;58. 59. 60. int32nRtn=MultiByteToWideChar(CP_UTF8,0,pu8,utf8Len,lpszW,nLen);61. 62. if(nRtn!=nLen)63. 64. deletelpszW;65. returnfalse;66. 67. 68. /convertanwidecharstringtoMultibyte69. int32MBLen=WideCharToMultiByte(CP_ACP,0,lpszW,nLen,NULL,0,NULL,NULL);70. if(MBLen=0)71. 72. returnfalse;73. 74. pmb.resize(MBLen);75. nRtn=WideCharToMultiByte(CP_ACP,0,lpszW,nLen,&*pmb.begin(),MBLen,NULL,NULL);76. deletelpszW;77. 78. if(nRtn!=MBLen)79. 80. pmb.clear();81. returnfalse;82. 83. returntrue;84. 85. 86. /多字节编码转为Unicode编码87. boolMBToUnicode(vector&pun,constchar*pmb,int32mLen)88. 89. /convertanMBCSstringtowidechar90. int32uLen=MultiByteToWideChar(CP_ACP,0,pmb,mLen,NULL,0);91. 92. if(uLen=0)93. 94. returnfalse;95. 96. pun.resize(uLen);97. 98. int32nRtn=MultiByteToWideChar(CP_ACP,0,pmb,mLen,&*pun.begin(),uLen);99. 100. if(nRtn!=uLen)101. 102. pun.clear();103. returnfalse;104. 105. returntrue;106. 107. 108. /Unicode编码转为多字节编码109. boolUnicodeToMB(vector&pmb,constwchar_t*pun,int32uLen)110. 111. /convertanwidecharstringtoMultibyte112. int32MBLen=WideCharToMultiByte(CP_ACP,0,pun,uLen,NULL,0,NULL,NULL);113. if(MBLen=0)114. 115. returnfalse;116. 117. pmb.resize(MBLen);118. intnRtn=WideCharToMultiByte(CP_ACP,0,pun,uLen,&*pmb.begin(),MBLen,NULL,NULL);119. 120. if(nRtn!=MBLen)121. 122. pmb.clear();123. returnfalse;124. 125. returntrue;126. 127. 128. /UTF8编码转为Unicode129. boolUTF8ToUnicode(vector&pun,constchar*pu8,int32utf8Len)130. 131. /convertanUTF8stringtowidechar132. int32nLen=MultiByteToWideChar(CP_UTF8,0,pu8,utf8Len,NULL,0);133. if(nLen=0)134. 135. returnfalse;136. 137. pun.resize(nLen);138. int32nRtn=MultiByteToWideChar(CP_UTF8,0,pu8,utf8Len,&*pun.begin(),nLen);139. 140. if(nRtn!=nLen)141. 142. pun.clear();143. returnfalse;144. 145. 146. returntrue;147. 148. 149. /Unicode编码转为UTF8150. boolUnicodeToUTF8(vector&pu8,constwchar_t*pun,int32uLen)151. 152. /convertanwidecharstringtoutf8153. int32utf8Len=WideCharToMultiByte(CP_UTF8,0,pun,uLen,NULL,0,NULL,NULL);154. if(utf8Len=0)155. 156. returnfalse;157. 158. pu8.resize(utf8Len);159. int32nRtn=WideCharToMultiByte(CP_UTF8,0,pun,uLen,&*pu8.begin(),utf8Len,NULL,NULL);160. 161. if(nRtn!=utf8Len)162. 163. pu8.clear();164. returnfalse;165. 166. returntrue;167. VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本Ansi字符串我们最熟悉,英文占一个字节,汉字2个字节,以一个0结尾,常用于txt文本文件Unicode字符串,每个字符(汉字、英文字母)都占2个字节,以2个连续的0结尾,NT操作系统内核用的是这种字符串,常被定义为typedef unsigned short wchar_t;所以我们有时常会见到什么char*无法转换为unsigned short*之类的错误,其实就是unicodeUTF8是Unicode一种压缩形式,英文A在unicode中表示为0x0041,老外觉得这种存储方式太浪费,因为浪费了50%的空间,于是就把英文压缩成1个字节,成了utf8编码,但是汉字在utf8中占3个字节,显然用做中文不如ansi合算,这就是中国的网页用作ansi编码而老外的网页常用utf8的原因。UTF8在还游戏里运用的很广泛,比如WOW的lua脚本等下面来说一下转换,主要用代码来说明吧写文件我用了CFile类,其实用FILE*之类的也是一样,写文件和字符串什么类别没有关系,硬件只关心数据和长度Ansi转Unicode介绍2种方法void CConvertDlg:OnBnClickedButtonAnsiToUnicode() / ansi to unicode char* szAnsi = abcd1234你我他; /预转换,得到所需空间的大小 int wcsLen = :MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0); /分配空间要给0留个空间,MultiByteToWideChar不会给0空间 wchar_t* wszString = new wchar_twcsLen + 1; /转换 :MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen); /最后加上0 wszStringwcsLen = 0; /unicode版的MessageBox API :MessageBoxW(GetSafeHwnd(), wszString, wszString, MB_OK); /接下来写入文本 /写文本文件,头2个字节0xfeff,低位0xff写在前 CFile cFile; cFile.Open(_T(1.txt), CFile:modeWrite | CFile:modeCreate); /文件开头 cFile.SeekToBegin(); cFile.Write(xffxfe, 2); /写入内容 cFile.Write(wszString, wcsLen * sizeof(wchar_t); cFile.Flush(); cFile.Close(); delete wszString; wszString =NULL; /方法2 /设置当前地域信息,不设置的话,使用这种方法,中文不会正确显示 /需要#include setlocale(LC_CTYPE, chs); wchar_t wcsStr100; /注意下面是大写S,在unicode中,代表后面是ansi字符串 /swprintf是sprintf的unicode版本 /格式的前面要加大写L,代表是unicode swprintf(wcsStr, L%S, szAnsi); :MessageBoxW(GetSafeHwnd(), wcsStr, wcsStr, MB_OK);Unicode转Ansi也是2种方法void CConvertDlg:OnBnClickedButtonUnicodeToAnsi() / unicode to ansi wchar_t* wszString = Labcd1234你我他; /预转换,得到所需空间的大小,这次用的函数和上面名字相反 int ansiLen = :WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); /同上,分配空间要给0留个空间 char* szAnsi = new charansiLen + 1; /转换 /unicode版对应的strlen是wcslen :WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL); /最后加上0 szAnsiansiLen = 0; /Ansi版的MessageBox API :MessageBoxA(GetSafeHwnd(), szAnsi, szAnsi, MB_OK); /接下来写入文本 /写文本文件,ANSI文件没有BOM CFile cFile; cFile.Open(_T(1.txt), CFile:modeWrite | CFile:modeCreate); /文件开头 cFile.SeekToBegin(); /写入内容 cFile.Write(szAnsi, ansiLen * sizeof(char); cFile.Flush(); cFile.Close(); delete szAnsi; szAnsi =NULL; /方法2 /和上面一样有另一种方法 setlocale(LC_CTYPE, chs); char szStr100; /注意下面是大写,在ansi中,代表后面是unicode字符串 /sprintf sprintf(szStr, %S, wszString); :MessageBoxA(GetSafeHwnd(), szStr, szStr, MB_OK);Unicode转UTF8void CConvertDlg:OnBnClickedButtonUnicodeToU8() / unicode to UTF8 wchar_t* wszString = Labcd1234你我他; /预转换,得到所需空间的大小,这次用的函数和上面名字相反 int u8Len = :WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); /同上,分配空间要给0留个空间 /UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存 char* szU8 = new charu8Len + 1; /转换 /unicode版对应的strlen是wcslen :WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL); /最后加上0 szU8u8Len = 0; /MessageBox不支持UTF8,所以只能写文件 /接下来写入文本 /写文本文件,UTF8的BOM是0xbfbbef CFile cFile; cFile.Open(_T(1.txt), CFile:modeWrite | CFile:modeCreate); /文件开头 cFile.SeekToBegin(); /写BOM,同样低位写在前 cFile.Write(xefxbbxbf, 3); /写入内容 cFile.Write(szU8, u8Len * sizeof(char); cFile.Flush(); cFile.Close(); delete szU8; szU8 =NULL;UTF8转UNICODEvoid CConvertDlg:OnBnClickedButtonU8ToUnicode() /UTF8 to Unicode /由于中文直接复制过来会成乱码,编译器有时会报错,故采用16进制形式 char* szU8 = abcd1234xe4xbdxa0xe6x88x91xe4xbbx96x00; /预转换,得到所需空间的大小 int wcsLen = :MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0); /分配空间要给0留个空间,MultiByteToWideChar不会给0空间 wchar_t* wszString = new wchar_twcsLen + 1; /转换 :MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen); /最后加上0 wszStringwcsLen = 0; /unicode版的MessageBox API :MessageBoxW(GetSafeHwnd(), wszString, wszString, MB_OK); /写文本同ansi to unicodeAnsi转换utf8和utf8转换Ansi就是上面2个的结合,把unicode作为中间量,进行2次转换即可VC+ utf-8 Unicode GB2312 编码转换#include#include#includeusingnamespacestd;voidunicodeToUTF8(constwstring&src,string&result)intn=WideCharToMultiByte(CP_UTF8,0,src.c_str(),-1,0,0,0,0);result.resize(n);:WideCharToMultiByte(CP_UTF8,0,src.c_str(),-1,(char*)result.c_str(),result.length(),0,0);voidunicodeToGB2312(constwstring&wstr,string&result)intn=WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,0,0,0,0);result.resize(n);:WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,(char*)result.c_str(),n,0,0);voidutf8ToUnicode(conststring&src,wstring&result)intn=MultiByteToWideChar(CP_UTF8,0,src.c_str(),-1,NULL,0);result.resize(n);:MultiByteToWideChar(CP_UTF8,0,src.c_str(),-1,(LPWSTR)result.c_str(),result.length();voidgb2312ToUnicode(conststring&src,wstring&result)intn=MultiByteToWideChar(CP_ACP,0,src.c_str(),-1,NULL,0);result.resize(n);:MultiByteToWideChar(CP_ACP,0,src.c_str(),-1,(LPWSTR)result.c_str(),result.length();voidprintByte(stringstr)inti=0;for(i=0;istr.length();i+)printf(%02X,(unsignedchar)str.at(i);printf(/n);voidwprintByte(wstringstr)inti=0;for(i=0;i00111111unsignedcharutf_one=0,utf_other=0x80;/用于位或置标UTF-8编码0x80-1000000wchar_ttmp_wchar=L0;/用于宽字符位置析取和位移(右移位)unsignedchartmp_char=L0;if(!src_wchar)/return(size_t)-1;for(;)/检测字节序列长度if(src_wchar=0x7f)/0x7f)&(src_wchar=0x7ff)/0x7ff)&(src_wchar=0xffff)/0111,111111110xffff)&(src_wchar0x1fffff)&(src_wchar0x3ffffff)&(src_wchar1;i-)/一个宽字符的多字节降序赋值tmp_char=(unsignedchar)(tmp_wchar&byte_other);/后位与byte_other00111111dest_stri-1=(tmp_char|utf_other);/在前面加-跟或tmp_wchar=6;/右移位/这个时候i=1/对UTF-8第一个字节位处理,/第一个字节的开头1的数目就是整个串中字节的数目tmp_char=(unsignedchar)(tmp_wchar&byte_one);/根据上面附值得来,有效位个数dest_str0=(tmp_char|utf_one);/根据上面附值得来1的个数/位值析取分组_End!returncount_bytes;intCDGQDialog:g_f_wcs_to_pchar(CString&wstr,char*p)wchar_twc=L1;charc10=1;/申请一个缓存size_tr=0;/size_tunsignedintegerResultofsizeofoperatorinti=0;intj=0;for(i=0;i1)for(size_tx=1;xr;x+)pj=cx;j+;/pj=0;return1;三转换实例voidCMytestDlg:OnBnClickedButton2()/TODO:在此添加控件通知处理程序代码CStringccId=L2007071王;CStringsql;charmySql100;memset(mySql,0,sizeof(mySql);sql.Format(Lselectcxrq,cxdw,dxrq,dxdw,fxrq,fxdw,cx,flxfromj_clxxwheretrainnum_info_id=%s,ccId);/wchar_tsql=L你;g_f_wcs_to_pchar(sql,mySql);C
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医保卡办理挂失与补换流程
- 二手房买卖合同签订前的房屋交易背景调查指南
- 离婚赔偿协议书:房产、车辆及经济补偿协议
- 离婚时财产分割及债务承担协议书格式
- 离婚协议签订后未完成离婚手续的财产分割执行合同
- 离婚协议书房贷款还款期限变更及权益保障合同
- 离婚财产分割协议范本:婚前婚后共同财产明确
- 离婚协议书模板:包含继承权与子女未来规划的协议
- 高净值离婚财产合理分配与子女教育支持合同范本
- 儿童抚养权变更及财产分割补充协议范本
- 居室环境的清洁与消毒
- ××领导班子及成员分析研判报告
- GB/T 9124.1-2019钢制管法兰第1部分:PN系列
- GB/T 2518-2008连续热镀锌钢板及钢带
- Frenchay构音障碍评定
- 第二讲国外教育评价的发展历程
- 教育学原理课后答案主编项贤明
- 建筑装饰施工技术-轻质隔墙工程施工课件(-)
- 语言领域核心经验《学前儿童语言学习与发展核心经验》
- 德国工业4.0与数字化制造课件
- 肉制品加工技术完整版ppt课件全套教程(最新)
评论
0/150
提交评论