zoukankan      html  css  js  c++  java
  • C++中char cstring string互相转换

    1.CString字符串连接,避免用char* strcpy这样的方式好的多.

    CString World(“World”);

    CString HelloWorld = Hello + World;

    2.格式化字符串

    CString s;

    s.Format(_T(“%d”),IntVar);

    _T(x)代表让字符有unicode兼容性

    3:Cstring 到 char* 类型的相互转化.

    CString HelloWorld = CString(“Hello”) + CString(“World”);

    4:char* 转化为CString

    char * p = “This is a test”;

    TCHAR * p = _T(“This is a test”);

    CString Hello(“Hello”);

    5:CString转化char*

    CString对象包含三个值,一个指向缓冲区的指针,一个该缓冲中有效的字符计数以及一个缓冲区的长度.

    LPCTSTR操作符(或者更明确地说就是const TCHAR*操作符)在CString类中被重载了.

    CString s(“Hello World”);

    LPCTSTR p = s;

    可以转化:

    CString s(_T(“HelloWorld”));

    LPTSTR p = s.GetBuffer();

    if(p != NULL)

    *p = _T(\0);

    s.ReleaseBuffer();

    在GetBuffer和ReleaseBuffer方法之间,不能调用CString对象任何方法,因为这样对象将无法保证完整性.

  • 相关阅读:
    我们用的信息系统安全吗?
    硬件代理服务器的日志分析方法
    Solarwinds Orion NPM实战视频演示
    网络资源管理系统LANsurveyor实战体验
    2013年,我的推荐博客汇总
    如何精准高效的实现视觉稿?------前端开发辅助工具AlloyDesigner使用介绍
    SVG如何做圆形图片
    canvas绘制一定数目的圆(均分)
    如何禁止火狐onblur时alert()产生类似选中的拖蓝效果
    ABCD多选正则表达式
  • 原文地址:https://www.cnblogs.com/sunliming/p/2068773.html
Copyright © 2011-2022 走看看