在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. dotnet core 运行程序注意事项

    .net core 程序 debug版本无法直接运行,因为没有相关的dll,只有在进入项目文件夹那一层,执行 dotnet run 来运行,会自动链接 当前用户的个人文件夹下的 .nuget/.pac ...

  2. 使用代码生成器“代码工厂”快速生成B/S程序代码

    开发目的: 自动生成C#.HTML.JS.Ajax 代码 .可以节省大量的时间来做业务逻辑的代码,那些重复的代码就不需要....了 环境支持: 硬件环境:window .VS2010+.支持SQLSe ...

  3. 如何在官网下载Spring jar包

    该链接里面讲的很仔细! http://blog.csdn.net/frankarmstrong/article/details/69808813

  4. day01_12.字符串

    1.字符转义 在字符串中,出现一些比较特殊的字符,容易引起歧义 我们需要转义这些引起歧义的字符串 <?php $a = 'ab\\c'; //转义字符2个 \' \\ $b = 'ab\'c'; ...

  5. day04_06 短路原则

    True和False不能写成ture和false,不然会报错 not not True or False and not True 按照not>and>or来进行括号 (not (not ...

  6. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  7. 【转载】用OCTAVE实现一元线性回归的梯度下降算法

    原文地址:http://www.cnblogs.com/KID-XiaoYuan/p/7247481.html STEP1 PLOTTING THE DATA 在处理数据之前,我们通常要了解数据,对于 ...

  8. kb-07专题线段树-04--离散化;

    /* poj2528 线段树 好题,用到了离散化,二分定位,特殊的区间查寻方式:在下面的代码注释中有详细的解释: */ #include<iostream> #include<cst ...

  9. 洛谷P3327 [SDOI2015]约数个数和 【莫比乌斯反演】

    题目 设d(x)为x的约数个数,给定N.M,求\(\sum_{i = 1}^{N} \sum_{j = 1}^{M} d(ij)\) 输入格式 输入文件包含多组测试数据.第一行,一个整数T,表示测试数 ...

  10. C# 打印webBrowser打开的页面

    this.webBrowser.Navigate(webBrowserUrl, tagerFrameName, postBuffer, heads); this.webBrowser.Document ...