// 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;
}
CString s;
int i = 64;
s.Format("%d", i)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有