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(一 ...
随机推荐
- nginx的常用负载均衡算法,分别是
随机分配,hash一致性分配,最小连接数分配,主备分配 随机,轮训,一致性哈希,主备,https://blog.csdn.net/liu88010988/article/details/5154741 ...
- luogu3808 luogu3796 AC自动机(简单版) AC自动机(加强版)
纪念一下我一晚上写了八遍AC自动机 这是加强版的: #include <iostream> #include <cstring> #include <cstdio> ...
- Spring Boot + Mybatis 多数据源配置实现读写分离
本文来自网易云社区 作者:王超 应用场景:项目中有一些报表统计与查询功能,对数据实时性要求不高,因此考虑对报表的统计与查询去操作slave db,减少对master的压力. 根据网上多份资料测试发现总 ...
- foy: 轻量级的基于 nodejs 的通用 build 工具
npm 的 scripts 下写的命令太多就很容易很乱,各种第三方轮子都只能解决一部分问题,总感觉不是很好用,想找个类似 make 的工具只能找到 jake, 可是 jake 的 API 太老,居然很 ...
- ACM程序设计选修课——1024: 末位零(求末尾0的方法+可有可无的快速幂)
1024: 末位零 Time Limit: 1 Sec Memory Limit: 32 MB Submit: 60 Solved: 11 [Submit][Status][Web Board] ...
- Python ping 模块
使用socket模块也可以获得域名对应的ip,参考:https://blog.csdn.net/c465869935/article/details/50850598 print socket.get ...
- element-ui 的 upload组件的clearFiles方法调用方法
<template> <div> <el-button @click="clearUploadedImage">重新上传</el-butt ...
- foj Problem 2275 Game
Problem D Game Accept: 145 Submit: 844Time Limit: 1000 mSec Memory Limit : 262144 KB Problem D ...
- 洛谷P1236 算24点
题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为“算24点”.您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数进行适当的算术运算, ...
- 【CF676D】Theseus and labyrinth(BFS,最短路)
题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门.每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等.现在给出起点和终点,每 ...