1:int转换为CString

CString str;
str.Format("As string: %d", int);

2:double转换为CString

CString str;
str.Format("As string: %g", double);

3:string转换为CString

CString str
std::string ss= "Hello,World!";
str= ss.c_str();
std::string s("Hello");
CString cs(s.c_str());

4:CString转换为string

CString cs("Hello");
std::string s((LPCTSTR)cs); //如果是UNICODE
CString cs ("Hello");
// 转换TCHAR string为 LPCSTR
CT2CA pszConvertedAnsiString (cs);
// 用LPCSTR构造一个string
std::string strStd (pszConvertedAnsiString);

5:CString转换为double

CString thestring("13.37");
double d = atof(thestring). //如果为Unicode编译, 使用_wtof():
CString thestring(L"13.37");
double d = _wtof(thestring). //同时支持Unicode和非Unicode
CString thestring(_T("13.37"));
double d = _tstof(thestring).

6:CString转换为int

int iValue;
CString cstrValue;
int i;
int j = swscanf_s(cstrValue, _T("%d"), &i);
if (j != )
{
// 未转换成功
iValue= ;
}
else
{
//转换成功
iValue= i;
}

7:CString转换为char*

CStringA m_szlstfile;
const size_t newsizea = (m_szlstfile.GetLength() + );
char *nstringa = new char[newsizea];
strcpy_s(nstringa, newsizea, m_szlstfile); CT2CA pszConvertedAnsiString(m_szlstfile);
char *nstringa = pszConvertedAnsiString; //const char *
const char *nstringa = m_szlstfile;

 8:转载自国外网站的CString转换整理

//UNICODE CString to std::wstring and std::string
CString cstrOrgString = L"SomeString";
//Converted Unicode CString to std::wstring
std::wstring wstring(cstrOrgString);
//Converted Unicode std::wstring to std::string
std::string strString;
strString.assign(wstring.begin(), wstring.end()); //UNICODE CString to std::string using ATL CW2A macros
CString cstrOrgString("SomeString");
std::string stdString(CW2A(cstrOrgString.GetString())); //UNICODE CString to std::string and UTF-8 encoded
std::string stdString(CW2A(cstrOrgString.GetString(), CP_UTF8)); //Converted from std::string to CString by using CString Constructor
std::string strStdString("SomeString");
CString strCString(strStdString.c_str()); //Converted from CString to std::string using ANSI variant CStringA to convert to char*; construct from it
CString strSomeCstring("SomeString");
std::string strStdString(CStringA(strSomeCstring)); //Multibyte CStringA string to a char * string
CStringA cstrMyString("SomeString");
const size_t newsizea = (cstrMyString.GetLength() + );
char *nstringa = new char[newsizea];
strcpy_s(nstringa, newsizea, cstrMyString); //Convert from CStringW to a char* string. To be safe, we allocate two bytes for each
//character in the original string, including the terminating null.
CStringW cstrMyString("SomeString");
const size_t newsizew = (cstrMyString.GetLength() + ) * ;
char *nstringw = new char[newsizew];
size_t convertedCharsw = ;
wcstombs_s(&convertedCharsw, nstringw, newsizew, cstrMyString, _TRUNCATE); //Convert CStringA to a wchar_t* wide string
CStringA cstrOrgStringA("SomeString");
size_t convertedCharsa = ;
wchar_t *wcstring = new wchar_t[newsizea];
mbstowcs_s(&convertedCharsa, wcstring, newsizea, cstrOrgStringA, _TRUNCATE); //Convert wide character CStringW string to a wide character wchar_t* string.
//To be safe, we allocate two bytes for each character in the original string, including the terminating null.
CStringW cstrOrgStringW("SomeString");
const size_t newsizew = (cstrOrgStringW.GetLength() + ) * ;
wchar_t *n2stringw = new wchar_t[newsizew];
wcscpy_s(n2stringw, newsizew, cstrOrgStringW); //Convert to a wide character _bstr_t string from a multibyte CStringA string.
CStringA cstrOrgStringA("SomeString");
_bstr_t bstrtString(cstrOrgStringA); //Convert to a wide character_bstr_t string from a wide character CStringW string.
CStringW cstrOrgStringW("SomeString");
_bstr_t bstrtwString(cstrOrgStringW); //Convert to a wide character CComBSTR string from a multibyte character CStringA string.
CStringA cstrOrgStringA("SomeString");
CComBSTR ccombstr(cstrOrgStringA); //Convert the wide character string to multibyte for printing.
CW2A printstr(ccombstr);
cout << printstr << endl; //Convert to a wide character CComBSTR string from a wide character CStringW string.
CStringW cstrOrgStringW("SomeString");
CComBSTR ccombstrw(cstrOrgStringW); //Convert the wide character string for printing
CW2A printstrw(ccombstrw);
wcout << printstrw << endl; //Convert a multibyte character CStringA to a multibyte version of a basic_string string (std::string).
CStringA cstrOrgStringA("SomeString");
std::string basicstring(cstrOrgStringA); //Convert a wide character CStringW to a wide character version of a basic_string (std::wstring) string.
std::wstring basicstringw(cstrOrgStringW); //Convert a multibyte character CStringA to a System::String.
CStringA cstrOrgStringA("SomeString");
String ^systemstring = gcnew String(cstrOrgStringA);
delete systemstring; //Convert a wide character CStringW to a System::String.
CStringW cstrOrgStringW("SomeString");
String^systemstringw = gcnew String(cstrOrgStringW);
delete systemstringw;

待续..

C++变量类型转换的更多相关文章

  1. javascript 数据类型 变量 类型转换运算符

    数据类型: 1.字符串(被双引号所包含的内容),小数,整数,日期时间,布尔型等. 2.变量: 都是通用类型的var,    定义一个变量格式:var a: 3.类型转换: 分为自动转换和强制转换,一般 ...

  2. 关于C语言中变量类型转换

    今天在工作中遇到一个问题,而在解决问题的过程中,发现一段关于int 型变量(a)和char型(b)变量间类型转换的代码存在问题:一个值为255的int型变量a,强制类型转换并赋值给char型变量b后, ...

  3. python中enumerate、变量类型转换

    enumerate可以在遍历过程中自动生成新的一列并从0开始计数 1 a = ["hello", "world", "dlrb"] 2 fo ...

  4. [Pytorch]Pytorch的tensor变量类型转换

    原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...

  5. PHP变量类型转换

    PHP数据类型转换 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: •(int).(integer):转换成整形 •(float).(double).(real):转换成浮点型 •(s ...

  6. java引用变量类型转换

    向上转型(子类→父类):(自动完成) 父类名称 父类对象 = 子类实例 ; 向下转型(父类→子类):(强制完成) 子类名称 子类对象 = (子类名称)父类实例 ; 对象名   instanceof  ...

  7. sas 变量类型转换

    data b2: set b1; newbl=put(oldbl,10.); run; 根据转换后的类型灵活填写

  8. PHP判断变量类型和类型转换的三种方式

    前言: PHP 在变量定义中不需要(不支持)明确的类型定义.变量类型是根据使用该变量的上下文所决定的.所以,在面对页码跳转.数值计算等严格的格式需求时,就要对变量进行类型转换. 举例如下: $foo ...

  9. 5.Powershell变量

    在指令执行过程中,会有一些数据产生,这些数据被用于以后的语句,需要一个存储单元暂时的存放这些数据,这个时候定义一个变量来存储数据.例如$string = “Hello Powershell!” Pow ...

随机推荐

  1. NVIDIA TensorRT 让您的人工智能更快!

    NVIDIA TensorRT 让您的人工智能更快! 英伟达TensorRT™是一种高性能深度学习推理优化器和运行时提供低延迟和高通量的深度学习推理的应用程序.使用TensorRT,您可以优化神经网络 ...

  2. BZOJ1566:[NOI2009]管道取珠——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1566 https://www.luogu.org/problemnew/show/P1758 题目 ...

  3. 51nod 1273 旅行计划(思维题)

    一开始看到这题真的有点懵逼...一直在想着套算法,结果题解除了sort和dfs其他什么都没用到 显然每次到达的一定都是叶子,先从根节点dfs一遍,按深度对叶子降序排序,按这个顺序向根节点dfs,路径上 ...

  4. Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  5. springboot集成junit测试与javamail测试遇到的问题

    1.springboot如何集成junit测试? 导入junit的jar包 使用下面注解: @RunWith()关于这个的解释看下这两篇文章: http://www.imooc.com/qadetai ...

  6. Hive分组后取组内排名方法row_number

    今天遇到这样一个需求场景,要取出 每一种分类(a,b组合分类) 符合条件的日期(字段c) 距离现在最近的10个日期 的数据 首先想到的是用sql筛选出符合某种条件的所有数据,这样的事情很简单 然后用脚 ...

  7. angularJs $resource自定义方法(待完善)

    配置CompanyService var services = angular.module('liaoyuan.services'); services.factory('CompanyServic ...

  8. jquery 条形码 插件jquery-barcode使用

    转载文章   jquery 条形码 插件jquery-barcode使用 条码官网: http://barcode-coder.com/en/barcode-jquery-plugin-201.htm ...

  9. 51Nod 1347 旋转字符串 | 规律

    Input示例 aa ab Output示例 YES NO 规律:abcabc 只需判断原始字符串 #include <bits/stdc++.h> using namespace std ...

  10. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...