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 ...
随机推荐
- BZOJ5334:[TJOI2018]数学计算——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5334 小豆现在有一个数x,初始值为1. 小豆有Q次操作,操作有两种类型: 1 m: x = x ...
- 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核
我们在前面已经介绍了coding.net和本地git的基本用法. 但是多人协作开发时情况会复杂得多,所以我们最好有一些规范来保证项目多人开发顺利进行. 比如说 规范一 master代码分支 需要开启 ...
- Python中的结构化数据分析利器-Pandas简介
Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发tea ...
- Educational Codeforces Round 6 B
B. Grandfather Dovlet’s calculator time limit per test 1 second memory limit per test 256 megabytes ...
- HDU1569 最大流(最大点权独立集)
方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 5869 Different GCD Subarray Query 树状数组+离线
Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...
- 2015/9/18 Python基础(14):函数式编程
这篇写了忘发.现在补上. Python不是也不大可能成为一种函数式的编程语言,但是它支持许多有价值的函数式编程语言构建.也有些表现的像函数式编程机制但是从传统上也不能认为是函数式编程语言的构建.Pyt ...
- MyBatis框架的使用及源码分析(八) MapperMethod
从 <MyBatis框架中Mapper映射配置的使用及原理解析(七) MapperProxy,MapperProxyFactory> 文中,我们知道Mapper,通过MapperProxy ...
- 【转载】Java JVM : Xms Xmx PermSize MaxPermSize 区别
转载自:http://cxh61207.iteye.com/blog/1160663 java JVM虚拟机选项: Xms Xmx PermSize MaxPermSize 区别 Xms 是指设定程 ...
- 【Foreign】字串变化 [DP]
字串变化 Time Limit: 10 Sec Memory Limit: 128 MB Description 定义一个(大写字母)字符串集合{S},初始时值包含一个给定的字符串S1,每次从中任意 ...