您的位置:逆风者 VC++ 正文
 添加时间:2007-09-01 原文发表:2007-08-31 人气:175 来源:vckbase.com

本文章共3946字,分3页,当前第1页,快速翻页:
 

UTF-8与GB2312之间的互换

作者:吴康彬

  相信一定有不少的程序开发人员时常会遇到字符编码的问题,而这个问题也是非常让人头痛的。因为这些都是潜在的错误,要找出这些错误也得要有这方面的开发经验才行。特别是在处理xml文档时 ,该问题的出现就更加的频繁了,有一次用java写服务器端程序,用vc写客户端与之交互。交互的协议都是用xml写的。结果在通讯时老是发现数据接受不正确。纳闷!于是用抓取网络数据包工具抓取数据,后来才发现原来是java上xml的头是这样的<?xml version="1.0" encoding="UTF-8"?>,而vc上默认的是GB2312。所以一遇到汉字数据就不正确了。去网上找资料,这方面的文章好象特别少,针对像这样的问题,下面我介绍一下我自己写的一个转换程序。当然,程序很简单。如果有画蛇添足的地方,还望各位高手一笑了之。
逆@风@者
  如果您对UTF-8、Unicode、GB2312等还是很陌生的话,请查看http://www.linuxforum.net/books/UTF-8-Unicode.html,我这里就不浪费口舌了。下面介绍一下WinAPI的两个函数:WideCharToMultiByte、MultiByteToWideChar。

函数原型:
int WideCharToMultiByte(

	UINT CodePage, // code page

	DWORD dwFlags, // performance and mapping flags

	LPCWSTR lpWideCharStr, // wide-character string

	int cchWideChar, // number of chars in string

	LPSTR lpMultiByteStr, // buffer for new string

	int cbMultiByte, // size of buffer

	LPCSTR lpDefaultChar, // default for unmappable chars

	LPBOOL lpUsedDefaultChar // set when default char used

); //将宽字符转换成多个窄字符



int MultiByteToWideChar(

	UINT CodePage, // code page

	DWORD dwFlags, // character-type options

	LPCSTR lpMultiByteStr, // string to map

	int cbMultiByte, // number of bytes in string

	LPWSTR lpWideCharStr, // wide-character buffer

	int cchWideChar // size of buffer

);//将多个窄字符转换成宽字符      
需要用到的一些函数:
CString CXmlProcess::HexToBin(CString string)//将16进制数转换成2进制

{

	if( string == "0") return "0000";

	if( string == "1") return "0001";

	if( string == "2") return "0010";

	if( string == "3") return "0011";

	if( string == "4") return "0100";

	if( string == "5") return "0101";

	if( string == "6") return "0110";

	if( string == "7") return "0111";

	if( string == "8") return "1000";

	if( string == "9") return "1001";

	if( string == "a") return "1010";

	if( string == "b") return "1011";

	if( string == "c") return "1100";

	if( string == "d") return "1101";

	if( string == "e") return "1110";

	if( string == "f") return "1111";

	return "";

}





CString CXmlProcess::BinToHex(CString BinString)//将2进制数转换成16进制

{

	if( BinString == "0000") return "0";

	if( BinString == "0001") return "1";

	if( BinString == "0010") return "2";

	if( BinString == "0011") return "3";

	if( BinString == "0100") return "4";

	if( BinString == "0101") return "5";

	if( BinString == "0110") return "6";

	if( BinString == "0111") return "7";

	if( BinString == "1000") return "8";

	if( BinString == "1001") return "9";

	if( BinString == "1010") return "a";

	if( BinString == "1011") return "b";

	if( BinString == "1100") return "c";

	if( BinString == "1101") return "d";

	if( BinString == "1110") return "e";

	if( BinString == "1111") return "f";

	return "";

}



int CXmlProcess::BinToInt(CString string)//2进制字符数据转换成10进制整型

{

	int len =0;

	int tempInt = 0;

	int strInt = 0;

	for(int i =0 ;i < string.GetLength() ;i   )

	{

	        tempInt = 1;

	        strInt = (int)string.GetAt(i)-48;

	        for(int k =0 ;k < 7-i ; k  )

	        {

			tempInt = 2*tempInt;

	        }

	        len  = tempInt*strInt;

	}

	return len;

}       
 
本文章更多内容1 - 2 - 3 - 下一页>>
相关文章

使用 Http 在线自动升级程序
《系统飞狐》中获取系统信息的方法
发掘 C# 特性赋予科学计算项目以威力
定制调试诊断工具和实用程序——摆脱DLL“地
软件开发专业相关的英文简历及其资源
VC 6.0 中如何使用 CRT 调试功能来检测内
完美实现真彩自绘菜单
控件如何传递自定义struct
文件系统识别器
用 ATL ActiveX 绘制任意平面函数的曲线
在VC中调用DirectShow全屏播放视频
案例研究:If-Trader 订单流程处理系统
介绍一个操作DHTML表格的C 对象
在非MFC程序中引用CString
简单PID数字控制的VC程序实现
论软件接口中几种底层通讯的实现
清理VC工程
VC.NET简单实现GIF动画
MFC Activex 录音机控件
VC Oracle 开发入门

相关评论


本文章所属分类:首页 VC++

  热门关键字: