ANSI、UNICODE和UTF8之间字符转换

 

文件头

 


 

 

#ifndef _CODE_TRANS_H_

#define _CODE_TRANS_H_

 

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

 

#include <string>

using namespace std;

 

//XML code convert

namespace Code_Trans

{

     /*************************************************************************

     * Name        : Unicode2UTF8

     * Description: Convert Unicode to UTF8 code

     * input       : ptszUnicode, point to the wide bytes

     * output : N/A

     * result : The converting result

     *************************************************************************/

     string Unicode2UTF8(const wchar_t* ptszUnicode);

 

     /*************************************************************************

     * Name        : Unicode2ANSI

     * Description: Convert Unicode to ANSI code

     * input       : ptszUnicod, point to the wide bytes

     * output : N/A

     * result : The converting result

     *************************************************************************/

     string Unicode2ANSI(const wchar_t* ptszUnicode);

 

     /*************************************************************************

     * Name        : ANSI2Unicode

     * Description: Convert ANSI code to Unicode

     * input       : N/A

     * output : N/A

     * result : The converting result

     *************************************************************************/

     wstring ANSI2Unicode(const char* pszAnsi);

 

     /*************************************************************************

     * Name        :

     * Description: Convert ANSI code to UTF8 code

     * input       : pszANSI, point to the multi bytes

     * output : N/A

     * result : The converting result

     *************************************************************************/

     string ANSI2UTF8(const char* pszANSI);

 

     /*************************************************************************

     * Name        : UTF*2ANSI

     * Description: Convert UTF8 code to ANSI code

     * input       : pszUTF, point to UTF8 bytes

     * output : N/A

     * result : The converting result

     *************************************************************************/

     string UTF82ANSI(const char* pszUTF);

 

     /*************************************************************************

     * Name        : UTF82Unicode

     * Description: Convert UTF8 code to Unicode

     * input       : pszUTF, point to UTF8 bytes

     * output : N/A

     * result : The converting result

     *************************************************************************/

     wstring UTF82Unicode(const char* pszUTF);

}//namespace Code_Trans

 

#endif

 

文件实现

 


 

#include "code_trans.h"

#include <windows.h>

 

namespace Code_Trans

{

/************************************************************************* 

* Name : Unicode2ANSI

* Description: Convert Unicode to ANSI code 

* Author : huaijun.yu

* Date : 2010/05/28

************************************************************************/

string Unicode2ANSI(const wchar_t* ptszUnicode)

{

string strRet;

if (NULL == ptszUnicode)

{

return strRet;

}

 

int iLen = WideCharToMultiByte(CP_ACP, 0, ptszUnicode, -1, NULL, 0, 0,FALSE);

if (iLen > 0)

{

char* pszBuf = new char[iLen+1];

if (NULL != pszBuf)

{

memset(pszBuf, 0, sizeof(char)*(iLen+1));

 

if (SUCCEEDED(WideCharToMultiByte(CP_ACP, 0, ptszUnicode, wcslen(ptszUnicode), 

pszBuf, iLen+1, 0, FALSE)))

{

strRet = pszBuf;

}

}

 

delete[] pszBuf;

pszBuf = NULL;

}

 

return strRet;

}

/************************************************************************* 

* Name : Unicode2UTF8

* Description: Convert Unicode to UTF8 code

* Author : huaijun.yu

* Date : 2010/05/27

************************************************************************/

string Unicode2UTF8(const wchar_t* ptszUnicode)

{

if (NULL == ptszUnicode)

{

return NULL;

}

 

int iSize = WideCharToMultiByte(CP_UTF8, 0, ptszUnicode, -1, NULL, 0, 0, FALSE);

if (iSize <=  0)

{

return NULL;

}

 

char *pszTemp = new char[iSize+1];

memset(pszTemp, 0, sizeof(char)*(iSize+1));

 

string strTemp;

if (SUCCEEDED(WideCharToMultiByte(CP_UTF8, 0, ptszUnicode, -1, pszTemp, iSize+1, 0, FALSE)))

{

strTemp = pszTemp;

}

 

delete[] pszTemp;

pszTemp = NULL;

 

return strTemp;

}

 

/************************************************************************* 

* Name : ANSI2Unicode

* Description: Convert ANSI code to Unicode

* Author : huaijun.yu

* Date : 2010/05/27

************************************************************************/

wstring ANSI2Unicode(const char* pszAnsi)

{

if (NULL == pszAnsi)

{

return NULL;

}

 

const int iLen = MultiByteToWideChar(CP_OEMCP, 0, pszAnsi, -1, 0, 0);

wchar_t *ptszTemp = new wchar_t[iLen+1];

memset(ptszTemp, 0, sizeof(wchar_t)*(iLen+1));

if (NULL == ptszTemp)

{

return NULL;

}

 

//convert

wstring wstrTemp;

if (SUCCEEDED(MultiByteToWideChar(CP_OEMCP, NULL, pszAnsi, strlen(pszAnsi), ptszTemp, iLen+1)))

{

wstrTemp = ptszTemp;

}

 

//free memory

delete[] ptszTemp;

ptszTemp = NULL;

 

return wstrTemp;

}

/************************************************************************* 

* Name : ANSI2UTF8

* Description: Convert ANSI code to UTF8 code

* Author : huaijun.yu

* Date : 2010/05/28

************************************************************************/

string ANSI2UTF8(const char* pszANSI)

{

string strRet;

wstring wstrTemp = ANSI2Unicode(pszANSI);

if (!wstrTemp.empty())

{

strRet = Unicode2UTF8(wstrTemp.c_str());

}

 

return strRet;

}

 

/************************************************************************* 

* Name : UTF82ANSI

* Description: Convert UTF8 code to ANSI code

* Author : huaijun.yu

* Date : 2010/05/28

************************************************************************/

string UTF82ANSI(const char* pszUTF)

{

string strRet;

 

wstring wstrTemp = UTF82Unicode(pszUTF);

if (!wstrTemp.empty())

{

strRet = Unicode2ANSI(wstrTemp.c_str());

}

 

return strRet;

}

 

/************************************************************************* 

* Name : UTF82Unicode 

* Description: Covert UTF8 code to Unicode

* Author : huaijun.yu

* Date : 2010/05/28

************************************************************************/

wstring UTF82Unicode(const char* pszUTF)

{

wstring wstrRet;

if (NULL == pszUTF)

{

return wstrRet;

}

 

int iSize = MultiByteToWideChar(CP_UTF8, 0, pszUTF, -1, NULL, 0);

if (iSize <=  0)

{

return wstrRet;

}

 

wchar_t *ptszTemp = new wchar_t[iSize+1];

memset(ptszTemp, 0, sizeof(wchar_t)*(iSize+1));

 

if (SUCCEEDED(MultiByteToWideChar(CP_UTF8, 0, pszUTF, strlen(pszUTF), ptszTemp, iSize+1)))

{

wstrRet = ptszTemp;

}

 

delete[] ptszTemp;

ptszTemp = NULL;

 

return wstrRet;

}

}//namespace Code_Trans;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值