unicode下各种类型转换,CString,string,char*,int,char[]

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

1stringCString

string a=”abc”;

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

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

 

2intCString

    Int a;

CString Cstr;

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

 

3char  CString 

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

例:

              char  szPath[];

              CString Cstr;

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

 

4CStringstring
              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());

 

5CStringconst 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;

 

6CStringchar*

       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;

 

 

7CStringint

              CString str=_T("123");

              int b=_ttoi(str);

              或者转成string,再转int

 

 

8char  string 
string s(char *);

 

9char[]LPWSTR

直接强制转换

 

10stringchar*

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;

 

11stringint

        string num;

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

 12 、int转string

#include

 

      stringstream ss;

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值