windows字符编码转换
来源:互联网 发布:程序员交流论坛 编辑:程序博客网 时间:2024/06/11 07:58
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CHINESECODE_H__E9206178_CA96_4B8A_AB2A_AD94C9CD12B1__INCLUDED_)
#define AFX_CHINESECODE_H__E9206178_CA96_4B8A_AB2A_AD94C9CD12B1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>
using namespace std;
class ChineseCode
{
public:
static void UTF_8ToUnicode(wchar_t* pOut,char *pText); // 把UTF-8转换成Unicode
static void UnicodeToUTF_8(char* pOut,wchar_t* pText); //Unicode 转换成UTF-8
static void UnicodeToGB2312(char* pOut,wchar_t uData); // 把Unicode 转换成 GB2312
static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 转换成 Unicode
static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 转为 UTF-8
static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 转为 GB2312
};
#endif // !defined(AFX_CHINESECODE_H__E9206178_CA96_4B8A_AB2A_AD94C9CD12B1__INCLUDED_)
// ChineseCode.cpp: implementation of the ChineseCode class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ChineseCode.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void ChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void ChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void ChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
return;
}
void ChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return ;
}
void ChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
int nLength = pLen* 3;
char* rst = new char[nLength];
memset(buf,0,4);
memset(rst,0,nLength);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
wchar_t pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = '\0';
//返回结果
pOut = rst;
delete []rst;
return;
}
void ChineseCode::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText[i] > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
newBuf[j] = '\0';
pOut = newBuf;
delete []newBuf;
return;
}
- windows字符编码转换
- Windows下字符编码转换
- Linux/Windows 下字符编码转换
- Windows下字符编码转换(ANSI <--> Unicode <--> UTF-8)
- 字符编码 编码转换 乱码
- 字符编码转换
- 字符编码转换
- Java字符编码转换
- 字符编码转换工具
- 字符编码转换函数
- java字符编码转换
- 字符编码转换
- 查表法字符编码转换
- 字符编码集转换
- 字符编码转换
- php 字符编码转换
- MultiByteToWideChar,字符编码转换
- 字符编码转换
- linux C C++ 字符集转换,UTF-8,GB2312
- phonics-8
- 创业公司CEO每周应该做的13件事
- 设备控制接口(ioctl 函数)
- max sum
- windows字符编码转换
- 用wubi安装ubuntu 10
- STL 适配器
- jqGrid与Struts2的结合应用(三) —— 使用自定义表单对话框编辑Grid数据
- centOS通过xen虚拟机安装XP系统的方法
- URL编码
- 电影 暂告安全 Safe
- 《乔布斯传》圈点(3)
- Github通过代理获取代码(Windows)