在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. luogu1972 [SDOI2009]HH的项链

    莫队裸题还不带修改 #include <algorithm> #include <iostream> #include <cstdio> #include < ...

  2. DataContext.ExecuteQuery的两种方法调用

    ExecuteQuery主要用于DataContext类直接执行SQL语句的查询,在MSDN上有两种执行方法,下面为两种方法的不同调用: 1.ExecuteQuery<TResult>(S ...

  3. LoadRunner 11破解方法

    名称:HP Loadrunner Software 11.00 版本号:11.00.0.0 安装环境:Win 7 软件安装成功后,会弹出提示告知license的有效期为10天. 破解方法: 1.下载破 ...

  4. 【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸

    多维数组声明 数据类型[][] 数组名称; 数据类型[] 数组名称[]; 数据类型数组名称[][]; 以上三种语法在声明二维数组时的功能是等价的.同理,声明三维数组时需要三对中括号,中括号的位置可以在 ...

  5. python中json.dump() 和 json.dumps() 有那些区别?

    JSON字符串用json.dumps, json.loads JSON文件名用json.dump, json.load 以下内容摘自:<Python Cookbook> json 模块提供 ...

  6. spring AOP详解二

    AOP实例(通过Proxy代理模式) Spring AOP使用纯java实现,不需要专门的编译过程和类装载器,它在运行期间通过代理方式向目标类织入增强代码,它更侧重于提供一种和Spring IoC容器 ...

  7. CCF第四题无向图打印路径 欧拉问题

    #include<iostream> #include<vector> #include<algorithm> #include<stack> #def ...

  8. python自动安装python2.7

    #coding = utf-8 import os import sys if(os.getuid() == 0): pass else: print ("you are not root ...

  9. ora-08104 该索引对象 159639 正在被联机建立或重建

    SSH远程连接数据库创建索引,网络中断后,删除索引信息报ora-08104 解决方法: 使用ONLINE_INDEX_CLEAN清除索引痕迹 在sys用户下执行 SQL> conn /as sy ...

  10. Welcome-to-Swift-08枚举 (Enumerations)

    枚举为一系相关联的值定义了一个公共的组类型.同时能够让你在编程的时候在类型安全的情况下去使用这些值. 如果你对C语言很熟悉,你肯定知道在C语言中枚举类型就是一系列具有被指定有关联名称的的整数值.但在S ...