zoukankan      html  css  js  c++  java
  • C89:论各种类型相互转换

    一.int

    1.int转float

    2.int转double

    3.int转string

    4.int转char*

    二.float

    1.float转int

    2.float转double

    3.float转string

    4.float转char*

    三.double

    1.double转int

    2.double转float

    3.double转string

    4.double转char*

    四.char* / char[]

    1.char*转int

    2.char*转float

    3.char*转double

    4.char* / char[]转string

    string s;
    char* p="hello";
    char a[]="hello";
     
    s=p;
    s=a;
    

    五.string

    1.string转int

    2.string转float

    3.string转double

    4.string转char* / char[]

    // 1
    string str="hello";
    const char* p=str.data();
    char* p=(char*)str.data();
    
    // 2
    string str="world";
    const char* p=str.c_str();
    char* p=(char*)str.c_str();
    
    // 3
    string str="test";
    char array[50];
    str.copy(array,48,0);  //48代表数组长度,0代表起始位置
    array[49]='';        //添加结束符
     
    char* p=new char[50]();
    str.copy(p,48,0);
    *(p+49)='';
    

    六.LPCWSTR

    1.LPCWSTR转string

    std::string Wchar2Ansi(LPCWSTR pwszSrc)
    {
        int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
        if (nLen <= 0)  return std::string("");
        char* pszDst = new char[nLen];
        if (NULL == pszDst)  return std::string("");
        WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
        pszDst[nLen -1] =0;
        std::string strTemp(pszDst);
        delete[]  pszDst;
    
        return strTemp;      
    }
    
  • 相关阅读:
    Pandas学习笔记,如何从DataFrame里选择一个Series
    数据结构_郝斌_数组
    数据结构_郝斌_预备知识2
    数据结构_郝斌_预备知识1
    数据结构_郝斌_目录
    数据结构_郝斌_链表
    Vocabulary Recitation 2020/04/08
    5月11号
    5月10号
    5月9号
  • 原文地址:https://www.cnblogs.com/k5bg/p/11089945.html
Copyright © 2011-2022 走看看