将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)); }};
- 将MFC CString转成STD string
- 将MFC CString转成STD string
- 转换MFC CString 到std::string
- 将MFC的CString 类型转化为C++标准数据类型std::string
- C++: Converting an MFC CString to a std::string
- C++: Converting an MFC CString to a std::string
- MFC Unicode 含中文 CString,LPCTSTR 转换成std::string
- unicode CString -> std::string
- CString 转为 std::string
- CString 转 std::string
- CString->std::string 例子
- CString->std::string 例子
- cstring转为std::string
- 谈CString和std::string的相互转化,MFC用std::fstream中文路径和中文名
- 实战c++中的string系列--std::string与MFC中CString的转换
- CString、std::string格式化字符串
- cstring ,std::string,lPCWSTR 等等
- CString 转化为 std::string
- shell中${ } 的一些特异功能
- test
- unity3d 调用C++中DLL文件
- spink_lock
- snort problem2
- 将MFC CString转成STD string
- Unable to instantiate activity ComponentInfo{com.xxx.xxx} java.lang.ClassNotFoundException
- poj1042
- c/c++,windows中各种字符的转换
- 善其事先利其器 Eclipse 开发 Cocos2d-x
- 黑马程序员——正则表达式
- 基于RTP和Android的视频传输的研究实现方法
- js 框架
- linux 1.2.13 网络解析--收报分析(二层至三层过程)