unicode下各种类型转换,CString,string,char*,int,char _d cstring char

把最近用到的各种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[];

CStringCstr;

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

4、CString转string
unicode下各种类型转换,CString,string,char*,int,char[]_d cstring char
CString C_str=_T("abc");

stringstr((LPCSTR)CStringA(C_str));

或stringstr=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=newchar(50);

c=b.GetString();

或者:

CStringFilePath=_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
strings(char*);

9、char[]转LPWSTR

直接强制转换

10、string转char*

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

11、string转int

string num;

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

12 、int转string

#include

stringstreamss;

intn=123;
stringstr;
ss<<n;
ss>>str;

  

爱华网本文地址 » http://www.aihuau.com/a/25101015/278664.html

更多阅读

转载 另类下水文:矛盾,成长的音符

原文地址:另类下水文:矛盾,成长的音符作者:兴化陆泉根矛盾,成长的音符回到家,看着母亲,我有些心虚,满脸通红,不得不躲避着母亲搜索的眼神。母亲生性敏感,摸着我的脑门:咦,这孩子咋了,滚烫的,发热了?

各种肤质,诚心推荐的产品,希望有一款适合你~~~ 推荐一款耳机

这篇文章构思了很久,把适合各种肤质,高中低三档价位的产品都列出来了,希望真的能帮助到亲们先来说说我自己的肤质情况吧~~~年龄:肤质:曾经是完全的干皮,经过这几年的坚持护肤,现在是混合偏干,夏季T区会比较容易出油。熬夜太过频繁的话,偶

声明:《unicode下各种类型转换,CString,string,char*,int,char _d cstring char》为网友揑嗻事分享!如侵犯到您的合法权益请联系我们删除