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));
}
};