CString string char* char 之间的字符转换(多种方法)
在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换。首先解释下三者的含义。
CString 是一种很有用的数据类型。它们很大程度上简化了MFC中的许多操作(适用于MFC框架),使得MFC在做字符串操作的时候方便了很多。需要包含头文件#include <afx.h>
C++是字符串,功能比较强大。要想使用标准C++中string类,必须要包含#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件。Char * 专门用于指以'\0'为结束的字符串.
以下方法来进行转换:
// CharConvert.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include<afx.h>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//此方法适用于“多字节” 否则会出现'strcpy' : cannot convert parameter 2 from 'unsigned short *' to 'const char *'
/*CString str("Hello zzu");
cout<<"the CString is "<<str<<endl;
char a[100];
strcpy(a,str);
cout<<"the convert to char * is "<<a<<"(char *)"<<endl;*/ //CString 转换成string
// CString c_str1("Hello Zhengzhou University");
//string str;
//str=c_str1.GetBuffer(0);
//c_str1.ReleaseBuffer(); //否则就没有释放缓冲区所占的空间
//cout<<str<<endl;
//cout<<"\n"<<c_str1<<endl; //string 转换成CString
/*string str1("Hello College of Information Engineering");
CString c_str2;
c_str2=str1.c_str(); //c_str():生成一个const char*指针,指向以空字符终止的数组。
cout<<"the CString is "<<c_str2<<endl;
cout<<"the string is "<<str1<<endl;*/ //string转换成const char*
//方法一:
//string str2("Hello College of Information Engineering");
//const char * str3; //常数指针
//str3=str2.c_str();
//cout<<"string is "<<str2<<endl;
//cout<<"const char is "<<str3<<endl; //方法二:
// /* string str("Hello College of Information Engineering");
//const char * c_str;
//c_str=str.data();
//cout<<"string is "<<str<<endl;
//cout<<"const char is"<<c_str<<endl;*/ //string 直接转换成char*
///*string s1 = "abcdefg";
//char *data;
//int len = s1.length();
//data = (char *)malloc((len)*sizeof(char));
//s1.copy(data,len,0);
//cout<<len<<endl;
//cout<<data<<endl;*/ //string.copy()的用法
//size_t length;
// char buffer[8];
// string str("Test string......");
//
// length=str.copy(buffer,7,6); //从buffer6,往后数7个,相当于[ buffer[6], buffer[6+7] )
// buffer[length]='\0'; //加上'\0'使得buffer就到buffer[length]为止; // cout <<"buffer contains: " << buffer <<endl;
//char * 转换成string //char *到string
/* char * c_str="zzu";
string str(c_str);
cout<<"the c_str "<<c_str<<endl;
cout<<"the string is"<<str<<"and length is "<<str.length()<<endl;*/ //char a[]="asd";
//cout<<strlen(a)<<endl; //为什么显示的是3,不是有一个\0吗 //char * 到CString ////char * c_str1="zzu";
////CString str; //可以直接转换,因为CString存在重载(在多字节字符集下适用)
////str.Format("%s",c_str1);
////cout<<"the c_str1 is"<<c_str1<<endl;
////cout<<"the CString is"<<str<<endl; //方法1:使用API:WideCharToMultiByte进行转换(使用过,有效)
//CString str= CString("This is an example!");
//int n = str.GetLength(); //按字符计算,str的长度
//int len = WideCharToMultiByte(CP_ACP,0,str,n,NULL,0,NULL,NULL);//按Byte计算str长度
//char *pChStr = new char[len+1];//按字节为单位
//WideCharToMultiByte(CP_ACP,0,str,n,pChStr,len,NULL,NULL);//宽字节转换为多字节编码
// pChStr[len] = '\0';//不要忽略末尾结束标志
//用完了记得delete []pChStr,防止内存泄露
return 0;
}
同时需要注意的是,我们在平成写程序时,最好搞清我们的编译环境中的编码方式,不同的编码方式可以会导致一些字符转换失败,当我们编程开始,就要想好在哪个编码方式下进行,以免最后出现换编码方式了,代码却出现很多错误(很让人头疼),比如sqlite数据库中的中文字符乱码问题就是由于编码方式和数据库中默认的编码方式不一致。这点在我们写程序时一定谨记。
CString string char* char 之间的字符转换(多种方法)的更多相关文章
- c# asp.net core取当月第一天和最后一天及删除最后一个字符的多种方法
当月第一天0时0分0秒 DateTime.Now.AddDays( - DateTime.Now.Day).Date 当月最后一天23时59分59秒 DateTime.Now.AddDays( - D ...
- MFC中cstring,string和char[]的相互转化
int 转 CString:CString.Format("%d",int);...............................string 转 CString CSt ...
- CString,string和char*
CString是MFC中的 标准C中没有string,有string.h头文件,其中是strcpy,strcmp等函数.但操作对象都是char*类型 string是C++中封装的 转化:LPCSTR ...
- c++ string 和wstring 之间的互相转换函数
#include <string> std::string ws2s(const std::wstring& ws) { std::string curLocale = setlo ...
- C++ 将 std::string 转换为 char*
参考: std::string to char* C++ 将 std::string 转换为 char* 目前没有直接进行转换的方法.必须通过string对象的c_str()方法,获取C-style的 ...
- JAVA中java.util.Date、java.sql.Timestamp和String之间的互相转换
java.util.Date与的String互转 java.util.Date---->String /** * 将java.util.Date对象转化为String字符串 * @param d ...
- 通过编写串口助手工具学习MFC过程——(三)Unicode字符集的宽字符和多字节字符转换
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- CString,string,char*之间的转换(转)
这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...
- 【转载】CString,string,char*之间的转换
本文转自 <> 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌 ...
随机推荐
- PowerDesigner之PDM(物理概念模型)
一.PDM概述 PDM(物理数据模型),通俗地理解,就是在PowerDesigner中以图形化的方式展示和设计数据库. PDM中涉及到的基本概念包括: 表: 列: 视图: 主键: 候选键: 外键: 存 ...
- HTML+css实现图片全屏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vs 异常处理
解决方法: 4.0 删除 C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files 2..0 删除 C:/WINDOW ...
- C#中string类型前加@标志的作用
转自:http://stackoverflow.com/questions/4879152/c-sharp-before-a-string (stackoverflow) string字符串前加@ ...
- 一款安卓ShowcaseView视图源码效果
该源码是从源码天堂那边转载过来的,大家可以看看一下吧啊,一款安卓ShowcaseView视图源码效果,非常不错的,特别是在做引导时使用. 源码下载地址:http://code.662p.com/vie ...
- Ubuntu16.04安装JDK
转载请注明源出处:http://www.cnblogs.com/lighten/p/6105463.html 1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是 ...
- Memento
#include <iostream> #include <string> using namespace std; class Memento { public: Memen ...
- SQLServer存储过程入门
1.创建一个返回结果集的存储过程 create procedure firstpro As begin select * from dbo.Person End 执行: execute dbo.fir ...
- redis的安装过程基本配置及遇到问题的解决
下载软件包 在centos下如果没有wget先安装 wgetyum -y install wgetwget http://download.redis.io/releases/redis-3.0.0. ...
- php的swoole扩展中onclose和onconnect接口不被调用的问题
在用swoole扩展写在线聊天例子的时候遇到一个问题,查了不少资料,现在记录于此. 通过看swoole_server的接口文档,回调注册接口on中倒是有明确的注释: * swoole_server-& ...