C++变量类型转换
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++变量类型转换的更多相关文章
- javascript 数据类型 变量 类型转换运算符
数据类型: 1.字符串(被双引号所包含的内容),小数,整数,日期时间,布尔型等. 2.变量: 都是通用类型的var, 定义一个变量格式:var a: 3.类型转换: 分为自动转换和强制转换,一般 ...
- 关于C语言中变量类型转换
今天在工作中遇到一个问题,而在解决问题的过程中,发现一段关于int 型变量(a)和char型(b)变量间类型转换的代码存在问题:一个值为255的int型变量a,强制类型转换并赋值给char型变量b后, ...
- python中enumerate、变量类型转换
enumerate可以在遍历过程中自动生成新的一列并从0开始计数 1 a = ["hello", "world", "dlrb"] 2 fo ...
- [Pytorch]Pytorch的tensor变量类型转换
原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...
- PHP变量类型转换
PHP数据类型转换 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: •(int).(integer):转换成整形 •(float).(double).(real):转换成浮点型 •(s ...
- java引用变量类型转换
向上转型(子类→父类):(自动完成) 父类名称 父类对象 = 子类实例 ; 向下转型(父类→子类):(强制完成) 子类名称 子类对象 = (子类名称)父类实例 ; 对象名 instanceof ...
- sas 变量类型转换
data b2: set b1; newbl=put(oldbl,10.); run; 根据转换后的类型灵活填写
- PHP判断变量类型和类型转换的三种方式
前言: PHP 在变量定义中不需要(不支持)明确的类型定义.变量类型是根据使用该变量的上下文所决定的.所以,在面对页码跳转.数值计算等严格的格式需求时,就要对变量进行类型转换. 举例如下: $foo ...
- 5.Powershell变量
在指令执行过程中,会有一些数据产生,这些数据被用于以后的语句,需要一个存储单元暂时的存放这些数据,这个时候定义一个变量来存储数据.例如$string = “Hello Powershell!” Pow ...
随机推荐
- BZOJ5288 & 洛谷4436 & LOJ2508:[HNOI/AHOI2018]游戏——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5288 https://www.luogu.org/problemnew/show/P4436 ht ...
- BZOJ4245 [ONTAK2015]OR-XOR 【贪心】
题目链接 BZOJ4245 题解 套路① 位运算当然要分位讨论,高位优先 考虑在\(or\)下,如果该位为\(0\),则每一位都为\(0\) 套路② 我们选m段异或和,转化为\(m\)个前缀和的点,且 ...
- HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)
HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...
- layui中对表格操作按钮集的判断
可用如下语法: {{# if(d.IsAudit==false){ }} <a class='layui-btn layui-btn-xs layui-btn-normal' lay-event ...
- 题解【bzoj2427 [HAOI2010]软件安装】
Description 现在我们的手头有\(N\)个软件,对于一个软件\(i\),它要占用\(W_i\)的磁盘空间,它的价值为\(V_i\).我们希望从中选择一些软件安装到一台磁盘容量为\(M\)计算 ...
- UVA 10201 DP
Adventures in Moving - Part IV 题意: 汽车邮箱容量200升,最初有100升油,要求到达终点油箱中的油不少于100升的最小花费,不能到达终点输出Impossible. 汽 ...
- Mac 访问隐藏文件方法! 网上方法在我电脑上都不可用!
百度的方法: 如果你想打开整个系统的隐藏文件可以在终端下输入以下命令 defaults write com.apple.finder AppleShowAllFiles -bool true 关闭显示 ...
- SqlServer中循环和条件语句示例!
-- ╔════════╗ -- =============================== ║ if语句使用示例 ║ -- ╚════════╝ declare @a int set @a=12 ...
- ListView - SimpleAdapter 行间颜色交替(转)
一.概述 通过扩展SimpleAdapter,来改变显示外观.因为要每行的显示颜色,首先要获得每行的View实例,然后调用setBackgroundColor函数设置. 二.实例 [效果] [代码片段 ...
- executeQuery与executeUpdate
executeQuery就是查 executeUpdate是更新,那就是增删改,和新建 吗?