zoukankan      html  css  js  c++  java
  • c/c++不同字符类型转换

    在vc   gcc中,常常遇到各种字符类型,各种不同字符类型转换就成了一个大问题,下面是一个总结

    1.
    1.1 char*2int
    char *numstr = "1234"; int val = atoi(numstr);

    1.2 char*2double
    char *numstr = "12.34"; double val = atof(numstr);

    2.CString2LPCTSTR
    char *p="fdl";
    CString strd(p);
    LPCTSTR lps = (LPCTSTR)strd;

    3. LPCTSTR2CString
    LPCTSTR lpctStr;
    CString strTMP=lpctStr;//不要用CStringA

    4. CString2string
    1)
    CString strMfc="test";
    std::string strStl;
    strStl=strMfc.GetBuffer(0);
    2)
    //wchar_t2char定义见wchar_t2char中
    CString tem_cs=_T("fds");
    string * tem_s;
    wchar_t * wchar_t_c;
    char * char_c=new char(100);
    wchar_t_c=tem_cs.GetBuffer(0);
    wchar_t2char(wchar_t_c,char_c);
    tem_s=new string(char_c);

    5.string2CString
    CString strMfc;
    std::string strStl="test";
    strMfc=strStl.c_str();

    6.string2LPCTSTR
    1)
    string s("sdaf");
    CString cs;
    cs=s.c_str();
    LPCTSTR  lp=(LPCTSTR)cs;
    2)
    char*  string2LPCTSTR::Convert(string sFrom)
    {
    _TCHAR* sBuff = new _TCHAR[sFrom.length() + 2];
    int iLength = 0;
    iLength = wsprintf (sBuff,sFrom.c_str ());
    sBuff[iLength+1] = '\0';
    return sBuff;
    }

    7.int2LPCTSTR
    int i=3;
    CString   display_id_str;
    display_id_str.Format(_T("%d"),i);
    LPCTSTR  display_id=display_id_str;

    8.int2CString
    int i=3;
    CString   display_id_str;
    display_id_str.Format(_T("%d"),i);

    9CString2int
    1)
    CString ss="1212";
    int temp=atoi(ss);
    2)wchar_t
    CString ss="1212";
    int temp=_ttoi(ss);

    11wchar_t2char
    1)
    wchar_t* wchar_t_c=_T("dsdsds");
    char* char_c=new char(100);
    public void wchar_t2char(wchar_t* wchar_t_c,char* char_c)
    {
    int nLen = WideCharToMultiByte( CP_ACP, 0, wchar_t_c, -1, NULL, 0, NULL, NULL );
    if (nLen == 0)
    {
    return ;
    }
    WideCharToMultiByte( CP_ACP, 0, wchar_t_c, -1, char_c, nLen, NULL, NULL );
    }
    2)
    wchar_t   wchar_t_c[1024]={"wchar_t_c"};
    char   char_c[2048]={"char_c"};
    swprintf(wchar_t_c,L"%s",char_c);
    sprintf(char_c,"%s",wchar_t_c);

    12string2char*
    1)

    2)
    string s="ds";
    const char *p = s.c_str();

    13.char*2string
    char * t="ses";
    string s(t);
    14char2wchar_t
    wchar_t* wchar_t_c=new wchar_t(100);
    char* char_c="dsdsds";
    int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, char_c, -1, NULL, 0 );
    if (nLen == 0)
    {
    return ;
    }
    MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, char_c, -1, wchar_t_c, nLen );

    更多类型见 http://cffile.sinaapp.com/?p=75

  • 相关阅读:
    erwin逆向工程,logical模型列名修改为中文
    [Leetcode] Two pointer-- 76. Minimum Window Substring
    [Leetcode] Binary search tree -- 173. Binary Search Tree Iterator
    [Leetcode] 684. Redundant Connection
    [Leetcode] Binary tree--653. Two Sum IV
    [Leetcode] Binary tree -- 617. Merge Two Binary Trees
    [Leetcode] Binary tree-- 563. Binary Tree Tilt
    [Leetcode] Binary tree-- 572. Subtree of Another Tree
    [Leetcode] Binary tree-- 437. Path Sum III
    [Leetcode] Binary tree-- 113. Path Sum II
  • 原文地址:https://www.cnblogs.com/chenzhihong/p/2254826.html
Copyright © 2011-2022 走看看