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. 2018牛客多校第五场 H.subseq

    题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...

  2. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. 从零开始学Linux系统(五)用户管理和权限管理

    权限管理: 常识: chmod  U-所有者  g-所属组  O-其他人r-4-可读  w-2-可写  x-1-可执行  s-4-SetUID    s-2-SetGID    t-1-粘着位 注:目 ...

  4. poj2060——Taxi Cab Scheme(最小路径覆盖)

    Description Running a taxi station is not all that simple. Apart from the obvious demand for a centr ...

  5. 最新eclipse安装SVN插件

    转载自:http://welcome66.iteye.com/blog/1845176 eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 ...

  6. 【C++ STL】Vector

    1.结构 vector模塑出一个动态数组,因此,它本身是“将元素置于动态数组中加以管理”的一个抽象概念.vector将其元素复制到内部的dynamic array中.元素之间总存在某种顺序,所以vec ...

  7. PHP 数据加密

    <?php /** * * 加密 * */ function lock_url($txt, $key = "aiteng") { $chars = "ABCDEFG ...

  8. 工作笔记 --->新疆统计分析添加市场管理员相关功能笔记

    先上一张大致需求的图 表信息 点击首页 “管理站点”时打开一个窗口 <a href="javascript:void(0);" onclick="javascrip ...

  9. Linux SSH 无密码登录

    1. ssh-keygen -t rsa 2. scp root@ip:/root/.ssh/id_rsa.pub ./id2 3. cat id2 >> authtorized_keys ...

  10. HDFS fs 基本命令

    https://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSystemShell.html#Overvie ...