MFC常用字符编码转换

MFC中几种字符串的理解

CStringT

CStringT为模板类,CString、CStringA、CStringW为其三种具体实现;

CStringA

该类在系统中定义为:
typedef ATL::CStringT< char, StrTraitMFC_DLL< char > > CStringA;
即该类型字符串中的字符实际上为==char==类型,用1个字节表示;

CStringW

该类型在系统中的定义为:
typedef ATL::CStringT< wchar_t, StrTraitMFC_DLL< wchar_t > > CStringW;
该类型字符串中的字符类型为==wchar_t==类型,称为宽字符类型,在不同环境中,宽字符类型具体长度不同,在MSVS中该类型用2个字节表示;

CString

该类型在系统中的定义为:
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
其中字符的类型为==TCHAR==,而TCHAR在系统中的定义为:

#ifdef  UNICODE     
typedef wchar_t TCHAR;  
#else   
typedef char TCHAR; 

由上述代码可以看出,当编码方式为UNICODE的时候,CString与CStringW一样;否则,CString与CStringA一样;
即CString中字符具体表示方式取决于当前环境的编码方式;

字符串之间的转换


从wchar_t转换成char*
    CStringW srcstrW;
    int dBufSize = WideCharToMultiByte(CP_OEMCP, 0, srcstrW, -1, NULL, 0, NULL, FALSE);
    char *pBuf = new char[dBufSize + 1];
    memset(pBuf, 0, dBufSize + 1);
    WideCharToMultiByte(CP_OEMCP, 0, srcstrW, -1, pBuf, dBufSize, NULL, FALSE);
    //pBuf为转换后的字符串指针

封装成函数:

/*  
函数名称:CStringW2char
函数说明:将CStringW字符串转换成char类型的指针
参数说明:CStringW类型的字符串
返回值:char类型指针
注意:使用后需释放内存;
*/      
char* CStringW2char(CString srcstrW)    
{   
    int dBufSize = WideCharToMultiByte(CP_OEMCP, 0, srcstrW, -1, NULL, 0, NULL, FALSE);
    char *pBuf = new char[dBufSize + 1];
    memset(pBuf, 0, dBufSize + 1);
    WideCharToMultiByte(CP_OEMCP, 0, srcstrW, -1, pBuf, dBufSize, NULL, FALSE);
    pBuf[dBufSize] = '\0';
    return pBuf;
}

由于char*与string之间的转换较为直接,为了避免内存分配释放问题,可将其封装为CStringW转换到string的函数;

/*
函数名称:CStringW2string
函数说明:将CStringW字符串转换成string字符串
参数说明:CStringW类型的字符串
返回值:string类型的字符串;
*/  
string CStringW2string(CString srcstrW)
{
    int dBufSize = WideCharToMultiByte(CP_OEMCP, 0, srcstrW, -1, NULL, 0, NULL, FALSE);
    char *pBuf = new char[dBufSize + 1];
    memset(pBuf, 0, dBufSize + 1);
    WideCharToMultiByte(CP_OEMCP, 0, srcstrW, -1, pBuf, dBufSize, NULL, FALSE);
    pBuf[dBufSize] = '\0';
    string tmpstr = pBuf;
    delete pBuf;
    pBuf = NULL;
    return tmpstr;
}

从char*转换成wchar_t
    char srcstr[str_len];
    int dBufSize = MultiByteToWideChar(CP_UTF8, 0, srcstr, -1, NULL, 0);
    WCHAR *pBuf = new WCHAR[dBufSize + 1];
    memset(pBuf, 0, dBufSize + 1);
    MultiByteToWideChar(CP_UTF8, 0, srcstr, -1, pBuf, dBufSize);
    //pBuf为转换后的字符串指针

封装成函数

/*
函数名称:char2wchar
函数说明:将char*类型指针转换成WCHAR*类型的指针;
参数说明:char*类型的指针
返回值:WCHAR*类型的指针
注意:使用后需释放内存,作为参数传入的指针未释放;
*/
WCHAR* char2wchar(char *pstr)
{
    int dBufSize = MultiByteToWideChar(CP_UTF8, 0, pstr, -1, NULL, 0);
    WCHAR *pBuf = new WCHAR[dBufSize + 1];
    memset(pBuf, 0, dBufSize + 1);
    MultiByteToWideChar(CP_UTF8, 0, pstr, -1, pBuf, dBufSize);
    pBuf[dBufSize] = '\0';
    return pBuf;
}

同样的,可以将由char*指针指向的字符串转换成CStringW类型的字符串

/*
函数名称:char2CStringW
函数说明:将char*类型指针转换成CStringW字符串,
参数说明:char*类型的指针
返回值:CStringW类型的字符串
注意:作为参数传入的指针未释放;
*/
CStringW char2CStringW(char *pstr)
{
    int dBufSize = MultiByteToWideChar(CP_UTF8, 0, pstr, -1, NULL, 0);
    WCHAR *pBuf = new WCHAR[dBufSize + 1];
    memset(pBuf, 0, dBufSize + 1);
    MultiByteToWideChar(CP_UTF8, 0, pstr, -1, pBuf, dBufSize);
    pBuf[dBufSize] = '\0';
    CStringW tmpstrW = pBuf;
    delete pBuf;
    return tmpstrW;
}   

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弹指间LDL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值