加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

CSting

发布时间:2020-12-14 04:29:58 所属栏目:大数据 来源:网络整理
导读:输出: (1) https://blog.csdn.net/overlord_bingo/article/details/60140224 不含中文的情况下: CString s("hello world"); wcout "s=" s.GetString() endl; wcout "s=" (LPCTSTR)s endl;类型转换 wcout "s=" s.GetBuffer() endl; 含有中文的情况下需

输出:

(1)

https://blog.csdn.net/overlord_bingo/article/details/60140224

不含中文的情况下:

CString s("hello world");

wcout << "s=" << s.GetString() << endl;

wcout << "s=" << (LPCTSTR)s << endl;类型转换
wcout << "s=" << s.GetBuffer()<< endl;

含有中文的情况下需要添加:

wcout.imbue(std::locale("chs"));

如下

CString s("hello world");

wcout.imbue(std::locale("chs"));

wcout << "s=" << s.GetString() << endl;

wcout << "s=" << (LPCTSTR)s << endl;
wcout << "s=" << s.GetBuffer()<< endl;

(2)CString是unicode编码

CString a = "test";
printf("%Sn",a);//输出test;

printf("%sn",a);//输出t;

?

指针:

?

类型转换:

(1)LPCTSTR是把CString类型的强制转换为常字符串 LPCTSTR相当于const char*;输出 :例如 CString str; 要用printf输出str printf("%s",(LPCTSTR)str);

(2)CString 值赋给已分配内存的char *。

    CString cstr1 = "ASDDSD";
    int strLength = cstr1.GetLength() + 1;
    char *pValue = new char[strLength];
    strncpy(pValue,cstr1,strLength);

(3)char[]、char*、string转换成CString

  1. ? ??char?*ch?=?"Hello";??
  2. ????string?s?=?"Wrold";??
  3. ? ? char? chr[]="name";
  4. ????CString?cstr1(ch),?cstr2(s.c_str()),?cstr3("Program"),cstr4(chr);
  5. cstr2.Format("%s",?s.c_str());
  6. cstr2.Format("%s",?s.data());

c_str()和data()区别是:前者返回带‘/0‘的字符串,后者则返回不带‘/0‘的字符串.

(4) CString转换成char[]、char*、string

? ? CString cstr =?" To?CString";?

? ? string? str=cstr.GetBuffer(0);

?

附注:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读