将MFC CString转成STD string

MFC拥有其自己的字符串:CSting。我们经常发现有必要将一个Cstring转换为C++标准库中的std::string,这个函数如下:


贴一段网上找到的代码

/************************************************
* transfer in character ansi, utf8, unicode
* @author rene/2011-5-13
*
* @notice if you use it into something mfc application, you will make sure to include it under these mcf's headers.
* or else, the application can't be compiled with a error message as below:
*    "fatal error C1189: #error :  WINDOWS.H already included.  MFC apps must not #include <windows.h>"
* for example: at least
*    #include <afxwin.h>;
*    #include <afxcmn.h>;
* and so on, and then
*    #include "StringConverter.h"
*
* if you use precompiled file, normally
*    #include "stdafx.h"
* and you can't also use it freely including it any where that you wanna
*
* @demo
std::string szAnsi = "abc123你我他";

std::wstring szUnicode = CStringConverter::Ansi2Unicode(szAnsi);
szAnsi = CStringConverter::Unicode2Ansi(szUnicode);

std::string szUft8 = CStringConverter::Unicode2Utf8(szUnicode);
szUnicode = CStringConverter::Utf82Unicode(szUft8);

szAnsi = CStringConverter::Utf82Ansi(szUft8);
szUft8 = CStringConverter::Ansi2Utf8(szAnsi);
************************************************/
#pragma once

#include <string>
#include <wtypes.h>

class CStringConverter {
public:
    static std::wstring Ansi2Unicode(std::string szAnsi) {
        //calc block size to be returned
        int len = MultiByteToWideChar(CP_ACP, NULL, szAnsi.c_str(), szAnsi.size(), NULL, 0);
        //malloc and fill the returned block
        wchar_t* szUnicode = new wchar_t[len+1];
        MultiByteToWideChar(CP_ACP, NULL, szAnsi.c_str(), szAnsi.size(), szUnicode, len);
        szUnicode[len] = 0;
        std::wstring rs = szUnicode;
        delete[] szUnicode;
        
        return rs;
    }
    static std::string Unicode2Ansi(std::wstring szUnicode) {
        //calc block size to be returned
        int len = WideCharToMultiByte(CP_ACP, NULL, szUnicode.c_str(), szUnicode.size(), NULL, 0, NULL, NULL);
        //malloc and fill the returned block
        char* szAnsi = new char[len + 1];
        WideCharToMultiByte(CP_ACP, NULL, szUnicode.c_str(), szUnicode.size(), szAnsi, len, NULL, NULL);
        szAnsi[len] = 0;
        std::string rs = szAnsi;
        delete[] szAnsi;
        
        return rs;
    }
    
    static std::wstring Utf82Unicode(std::string szUtf8) {
        //calc block size to be returned
        int len = MultiByteToWideChar(CP_UTF8, NULL, szUtf8.c_str(), szUtf8.size(), NULL, 0);
        //malloc and fill the returned block
        wchar_t* szUnicode = new wchar_t[len+1];
        MultiByteToWideChar(CP_UTF8, NULL, szUtf8.c_str(), szUtf8.size(), szUnicode, len);
        szUnicode[len] = 0;
        std::wstring rs = szUnicode;
        delete[] szUnicode;
        
        return rs;
    }
    static std::string Unicode2Utf8(std::wstring szUnicode) {
        //calc block size to be returned
        int len = WideCharToMultiByte(CP_UTF8, NULL, szUnicode.c_str(), szUnicode.size(), NULL, 0, NULL, NULL);
        //malloc and fill the returned block
        char* szUtf8 = new char[len + 1];
        WideCharToMultiByte(CP_UTF8, NULL, szUnicode.c_str(), szUnicode.size(), szUtf8, len, NULL, NULL);
        szUtf8[len] = 0;
        std::string rs = szUtf8;
        delete[] szUtf8;
        
        return rs;
    }
    
    static std::string Ansi2Utf8(std::string szAnsi) {
        return Unicode2Utf8(Ansi2Unicode(szAnsi));
    }
    static std::string Utf82Ansi(std::string szUtf8) {
        return Unicode2Ansi(Utf82Unicode(szUtf8));
    }
};


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值