把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充:

1、string转CString

string a=”abc”;

CString str=CString(a.c_str());

或str.format("%s", a.c_str())

2、int转CString

Int a;

CString Cstr;

Cstr.Format(_T("%d"),a);

3、char 转 CString

CString.format("%s", char*);

例:

char  szPath[];

CString Cstr;

Cstr.Format(_T("%s"),szPath);

4、CString转string
              CString C_str=_T("abc");

string str((LPCSTR)CStringA(C_str));

或string str=CStringA(C_str);

或通过char*中转

CString m_str(_T("qwerg"));

char *chr=new char[m_str.GetLength()+1];

WideCharToMultiByte(CP_ACP,0,m_str.GetBuffer(),-1,chr,m_str.GetLength()+1,NULL,NULL);

string str=chr;

或者CString C_str = _T("ooqoqoq");

//   setlocale(LC_ALL, "chs");

char *p = new char[C_str.GetLength()+1];

wcstombs(p,C_str,C_str.GetLength()+1);

string str = p;

用下面代码检测:

MessageBox((CString)str.c_str());

5、CString转const char*

CString a=_T(“  ”);

CStringA b(a);

const char *c=new char(50);

c=b.GetString();

或者:

CString FilePath=_T(“   ”);

int len=WideCharToMultiByte( CP_ACP,0,FilePath.AllocSysString(),FilePath.GetLength(),NULL,0,NULL,NULL);

char * pAscii =new char[len];

len=WideCharToMultiByte(CP_ACP,0,FilePath.AllocSysString(),FilePath.GetLength(),pAscii,len+1,NULL,NULL);

pAscii[len]=0;

const char* path=(const char*)pAscii;

6、CString转char*

CString strPath = _T("啊啊啊");

int nLength = strPath.GetLength();

int nBytes = WideCharToMultiByte(CP_ACP,0,strPath,nLength,NULL,0,NULL,NULL);

char* VoicePath = new char[ nBytes + 1];

memset(VoicePath,0,nLength + 1);

WideCharToMultiByte(CP_OEMCP, 0, strPath, nLength, VoicePath, nBytes, NULL, NULL);

VoicePath[nBytes] = 0;

7、CString转int

CString str=_T("123");

int b=_ttoi(str);

或者转成string,再转int

8、char 转 string 
string s(char *);

9、char[]转LPWSTR

直接强制转换

10、string转char*

把string转换为char* 有3中方法:
1.data
如:
string str="abc";
char *p=str.data();
2.c_str
如:string str="gdfd";
    char *p=str.c_str();
3. copy
比如
string str="hello";
char p[40];
str.copy(p,5,0); //这里5,代表复制几个字符,0代表复制的位置
*(p+5)='\0'; //要手动加上结束符
cout <</span> <</span>p;

11、string转int

string num;

int a=atoi(num.c_str());

12 、int转string

#include

stringstream ss;

int n = 123;
    string str;
    ss<<n;
    ss>>str;

最新文章

  1. Linux常用指令指南,终端装逼利器
  2. [LintCode] Happy Number 快乐数
  3. Hibernate入门5持久化对象关系和批量处理技术
  4. Dreamweaver 时间轴如何打开
  5. Deep Learning for NLP 文章列举
  6. Java Tomcat 中调用.net DLL的方法
  7. C# 仿百度自动匹配
  8. NDEF消息解析实例[转]
  9. sqlserver常用sql语句,更改字段,建立唯一键,多个字段去重复等
  10. CCF-201312-1-出现次数最多的数
  11. XAMPP环境下配置Phalcon框架
  12. jetty去掉项目名称访问
  13. svn-经常遇到问题解答办法积累(一)
  14. react componentWillReceiveProps 使用注意
  15. e620. Activating a Keystroke When Any Component in the Window Has Focus
  16. 【费用流】【网络流24题】【P1251】 餐巾计划问题
  17. Dubbo注册中心Zookeeper安装步骤
  18. grideh SelectedRows Bookmark
  19. 下拉网页div自动浮在顶部
  20. 关于DeploymentConfig的思考

热门文章

  1. 该项目中不存在目标 precomputecompiletypescript The target &quot;PreComputeCompileTypeScript&quot; does not exist in the project
  2. cocoapods安装失败
  3. ubuntu用户及用户组文件信息
  4. 50 Pow(x, n)(求x的n次方Medium)
  5. Extjs中Chart利用series的tips属性设置鼠标划过时显示数据
  6. yum 搭建lnmp环境详细步骤
  7. python学习第二课要点记录
  8. 用soaplib的django webserver
  9. C++学习笔记3——类的封装(1)
  10. DNS递归和迭代原理