搜索
bottom↓
回复: 0

[转帖且备份]unicode下各种类型转换

[复制链接]

出0入0汤圆

发表于 2014-1-6 22:43:29 | 显示全部楼层 |阅读模式
http://blog.sina.com.cn/s/blog_a98e39a20101ari9.html

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

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-26 01:22

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表