System::String *,char*,string 等的类型转换 [转]
在VC 的编程中,经常会用到各种类型的转换,在MFC中textbox等控件得到的返回类型是System::String *,而写入的文件要求是 const char *类型的,下面介绍一些转换的方法:
string 转 CString CString.format("%s", string.c_str());
char* 转 CString CString.format("%s", char*);
char* 转 string string change=new string s(char *);
string 转 char * char *p = string.c_str();
CString转std::string
CString str = dlg.GetPathName();
setlocale(LC_ALL, "chs");
char *p = new char[256];
wcstombs( p, str, 256 );
m_fileName = p;
int 转CString而将数字转换为CString变量,
可以使用CString的Format函数
CString s;
int i = 64;
s.Format("%d", i)
Format函数的功能很强,值得你研究一下。
CString TO char *
要把CString转成char *,用操作符(LPCSTR
CString转换 char[100]
char a[100];
CString str("aaaaaa");
strncpy(a,(LPCTSTR)str,sizeof(a));
CString类型的转换成int
CString aaa = "16" ;
int int_chage = atoi((lpcstr)aaa) ;
char* 在装int
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);
System::String 转化成 char *类型(网上提供有许多种类)
1 MSDN上的
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >
using namespace System;
int main() {
String ^str = "Hello";
pin_ptr<const wchar_t> wch = PtrToStringChars(str);
printf_s("%S\n", wch);
size_t convertedChars = 0;
size_t sizeInBytes = ((str->Length 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err= wcstombs_s(&convertedChars, ch, sizeInBytes, wch,sizeInBytes);
if (err != 0) printf_s("wcstombs_s failed!\n");
printf_s("%s\n", ch);
}
2 网上找的
PtrToStringChars 指定了一个指向实际 String 对象的内部指针。如果将此指针传递给非托管函数调用,则必须先锁定该指针,以确保在进行异步垃圾回收过程中对象不会移动:
//#include <vcclr.h>
System::String * str = S"Hello world\n";
const __wchar_t __pin * str1 = PtrToStringChars(str);
wprintf(str1);
3 感觉这种最好用的
StringToHGlobalAnsi 将托管 String 对象的内容复制到本机堆,
然后动态地将它转换为美国国家标准学会 (ANSI) 格式。
此方法将分配所需的本机堆内存:
using namespace System;
using namespace System::Runtime::InteropServices;
System::String * str = S"Hello world\n";
char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
printf(str2);
System::String *,char*,string 等的类型转换 [转]的更多相关文章
- String -- char[]互转
1.String --> char[] String str = "abc"; char[] chs = str.toCharArray(); 2.char[] --> ...
- Lua的string和string库总结
Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...
- const char* && string && String^ 类型转换
const char* && string && String^ 类型转换 const char* ---> string const char * cw= &q ...
- C语言中string char int类型转换
C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...
- cocos2d-x类型转换(CCstring int string char UTF-8互转)
在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include ...
- 类型转换(CCstring int string char UTF-8互转)
在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include ...
- VS2013 MFC C++ CString ,const char , char, string 类型转换
VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...
- CString string char* char 之间的字符转换(多种方法)
在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换.首先解释下三者的含义. CString 是一种很有用的数据类型.它们很大程度上简化了MF ...
- Swift2.0 中的String(三):类型转换
本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...
随机推荐
- 评估后Vista时代系统内核模式安全性
Windows Vista与之前的MS Windows版本(包括WindowsXPSP2)相比增加了很多的安全性.Vista新安全性的特征可以包括以下几个方面: 驱动签名 路径保护 内核模式代码完整性 ...
- 【Shell】使用shell打印菜单,一键安装Web应用
问题描述: [解答] [root@A04-Test- scripts]# more menu.sh #!/bin/bash echo "1.[install lamp]" echo ...
- MySQL中的DDL(Data Definition Language,数据定义语言)
create(创建表) 标准的建表语句: create table [模式名.]表名 ( #可以有多个列定义 columnName1 dataType [default expr(这是默认值)], . ...
- AtCoder Regular Contest 091
数学场,做到怀疑人生系列 C - Flip,Flip, and Flip...... Time limit : 2sec / Memory limit : 256MB Score : 300 poin ...
- hdu1595 最短路问题(dijkstra&&spfa)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 九度oj 题目1188:约瑟夫环
题目描述: N个人围成一圈顺序编号,从1号开始按1.2.3......顺序报数,报p者退出圈外,其余的人再从1.2.3开始报数,报p的人再退出圈外,以此类推. 请按退出顺序输出每个退出人的原序号 ...
- openstack是什么?能干什么?
openstack是什么?能干什么?涉及的初衷是什么?由什么来组成?刚接触openstack,说openstack不是一个软件,而是由多个组件进行组合,这是一个更深层次的理解,当我们看到dashboa ...
- ecs01初始化node环境
npm install 报错 > uglifyjs-webpack-plugin@ postinstall /opt/apps/iview-admin/node_modules/webpack/ ...
- UVA 10131 Is Bigger Smarter?(DP最长上升子序列)
Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...
- Codeforces878C. Tournament
$n \leq 50000$个人,每个人有$K \leq 10$个属性,现对每一个前缀问:进行比赛,每次任意两人比任意属性,小的淘汰(保证同一属性不会出现两个相同的数),最终有几个人有可能获胜. 明显 ...