• 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对象任何方法,因为这样对象将无法保证完整性.

  • 相关阅读:
    lvs三种模式的优缺点对比
    linux下的$0-n作用
    图解 HTTP 笔记(一)——了解 Web 及网络基础
    从源码学习使用 node-delegates
    从源码看 Vue 中的 Mixin
    常见 Web 性能优化方式
    解密虚拟 DOM——snabbdom 核心源码解读
    浅谈 JSONP
    代码覆盖率测试及 GitHub 自动化集成
    Redis 学习笔记
  • 原文地址:https://www.cnblogs.com/sunliming/p/2068773.html
Copyright © 2020-2023  润新知