将MFC CString转成STD string

来源:互联网 发布:知乎 邮箱 编辑:程序博客网 时间:2024/04/28 13:05

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

std::string CColorDetectDlg::CStringToSTDstring(const CString& theCString){// Convert the CString to a regular char arrayconst int theCStrLen = theCString.GetLength();char *buffer = (char*)malloc(sizeof(char)*(theCStrLen+1));memset((void*)buffer, 0, sizeof(buffer));WideCharToMultiByte(CP_UTF8, 0, static_cast<CString>(theCString).GetBuffer(), theCStrLen, buffer, sizeof(char)*(theCStrLen+1), NULL, NULL);buffer[theCStrLen] = '\0';// Construct a std::string with the char arraystd::string stdstring(buffer);// free the memory used by the char arrayfree((void*)buffer);// return the std::string objectint n = stdstring.length();return stdstring;}

贴一段网上找到的代码

/************************************************* 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** @demostd::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));    }};


原创粉丝点击