Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2675594
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: 嵌入式

2014-09-01 15:43:09


利用此API进行编码转换

GB2312,BIG5,Unicode相互转换
   
//GB2312 转换成 Unicode: 
wchar_t* GB2312ToUnicode(const char* szGBString)
{
        UINT nCodePage = 936; //GB2312
        int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);
        wchar_t* pBuffer = new wchar_t[nLength+1];
        MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);
        pBuffer[nLength]=0;
        return pBuffer;
}
//BIG5 转换成 Unicode:
wchar_t* BIG5ToUnicode(const char* szBIG5String)
{
        UINT nCodePage = 950; //BIG5
        int nLength=MultiByteToWideChar(nCodePage,0,szBIG5String,-1,NULL,0);
        wchar_t* pBuffer = new wchar_t[nLength+1];
        MultiByteToWideChar(nCodePage,0,szBIG5String,-1,pBuffer,nLength);
        pBuffer[nLength]=0;
        return pBuffer;
}
//Unicode 转换成 GB2312:
char* UnicodeToGB2312(const wchar_t* szUnicodeString)
{
        UINT nCodePage = 936; //GB2312
        int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
        char* pBuffer=new char[nLength+1];
        WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
        pBuffer[nLength]=0;
        return pBuffer;
}
//Unicode 转换成 BIG5:
char* UnicodeToBIG5(const wchar_t* szUnicodeString)

        UINT nCodePage = 950; //BIG5
        int nLength=WideCharToMultiByte(nCodePage,0,
szUnicodeString,-1,NULL,0,NULL,NULL);
        char* pBuffer=new char[nLength+1];
        WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
        pBuffer[nLength]=0;
        return pBuffer;
}
//繁体中文BIG5 转换成 简体中文 GB2312
char* BIG5ToGB2312(const char* szBIG5String)
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),
SORT_CHINESE_PRC);
        wchar_t* szUnicodeBuff = BIG5ToUnicode(szBIG5String);
        char* szGB2312Buff = UnicodeToGB2312(szUnicodeBuff);
        int nLength = LCMapString(lcid,LCMAP_SIMPLIFIED_CHINESE, szGB2312Buff,-1,NULL,0);
        char* pBuffer = new char[nLength + 1];
        LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE,szGB2312Buff,-1,pBuffer,nLength);
        pBuffer[nLength] = 0;
        delete[] szUnicodeBuff;
        delete[] szGB2312Buff;
        return pBuffer;
}

//简体中文 GB2312 转换成 繁体中文BIG5
char* GB2312ToBIG5(const char* szGBString)
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),
SORT_CHINESE_PRC);
        int nLength = LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,NULL,0);
        char* pBuffer=new char[nLength+1];
        LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,pBuffer,nLength);
        pBuffer[nLength]=0;
        wchar_t* pUnicodeBuff = GB2312ToUnicode(pBuffer);
        char* pBIG5Buff = UnicodeToBIG5(pUnicodeBuff);
        delete[] pBuffer;
        delete[] pUnicodeBuff;
        return pBIG5Buff;
}

阅读(1247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~