在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 等的类型转换 [转]的更多相关文章

  1. String -- char[]互转

    1.String --> char[] String str = "abc"; char[] chs = str.toCharArray(); 2.char[] --> ...

  2. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  3. const char* && string && String^ 类型转换

    const char* && string && String^ 类型转换 const char* ---> string const char * cw= &q ...

  4. C语言中string char int类型转换

    C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...

  5. cocos2d-x类型转换(CCstring int string char UTF-8互转)

    在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include  ...

  6. 类型转换(CCstring int string char UTF-8互转)

    在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include  ...

  7. VS2013 MFC C++ CString ,const char , char, string 类型转换

    VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...

  8. CString string char* char 之间的字符转换(多种方法)

    在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换.首先解释下三者的含义. CString 是一种很有用的数据类型.它们很大程度上简化了MF ...

  9. Swift2.0 中的String(三):类型转换

    本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...

随机推荐

  1. HDU 3488 KM Tour

    参考题解 这题注意有重边.. #include <cstdio> #include <cstring> #include <algorithm> using nam ...

  2. day01_06.比较运算符

    >  >=  =  <  <=  != == ===  !== 凡运算,必有运算结果,比较运算符的运算结果是布尔值 ==和===的区别 <?php $c = ( 3 == ...

  3. 【转】Unity协程(Coroutine)原理深入剖析

    Unity协程(Coroutine)原理深入剖析 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 记得去年6月份刚开始实习的时候,当时要我写网 ...

  4. 【Luogu】P1430序列取数(DP)

    题目链接 博弈DP太喵了qwq 设f[i][j]表示剩下区间[i,j]要取,先手最大值 明显我们要从这区间里面拿个最大的 就等价于这段区间的前缀和,我们要给对手留下个最小的 就是f[i][j]=sum ...

  5. 刷题总结——Bob's Race(hdu4123 树形dp+st表)

    题目: Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the ro ...

  6. 【CF20C】Dijkstra?(DIJKSTRA+HEAP)

    没什么可以说的 做dijk+heap模板吧 以后考试时候看情况选择SFPA和DIJKSTRA ; ..]of longint; dis:..]of int64; a:..]of int64; b:.. ...

  7. net5:Theme主题样式的动态变换,在内容页content中操作影响模板页的操作

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  8. power shell remoting

    Powershell Remoting建立在windows WinRM服务之上,可以一对一或一对多远程控制,也可以建立HTTP 或 HTTPS的“listeners”,使用WS-MAM协议接收远程传递 ...

  9. sublime text3 cssrem 快速px转rem插件

    今天试验了下cssrem  在移动端如果需要px->rem非常方便 比较之前我自己用gulp提供的函数unit(70/@base,rem);转方便很多 1.git clone https://g ...

  10. 二、 java中的变量与数据类型及类型转换

    标识符:凡是可以自己命名的地方都叫标识符,如:类名.方法名.接口名... 1.标识符命名的规则: 由26个英文字母大小写,0-9,_或$组成,不遵守会报错. 不可以用数字开头. 不能使用关键字和保留字 ...