C++字符串之间转化——Unicode字符集

本文主要介绍:Unicode字符集下字符串之间以及字符串与数字之间的转化。
1.string、char*与 const char*
<1>string->char*

char *ctr = new char[str.length()+1];
    strcpy(ctr,str.c_str());
    delete[]ctr; //用完后释放字符串

<2>string->const char*

    string str("good");
    const char *p = str.c_str();//转化成const char*

<3>char*->string

    char *ctr = "good";
    string str(ctr);
    //或
    string str2 = ctr;

<4>char*->const char*

    char *ctr = "good";
    const char *ctr2 = ctr;

<5>const char*->string

    const char *ctr = "good";
    string str(ctr);

<6>const char*->char*

    const char *ctr = "good";
    char *ctr2= new char[strlen(ctr)+1] ;
    strcpy(ctr2, ctr);
    delete[]ctr; //用完后释放字符串
//对于char[]可以用char *指向它,进而实现其它转化

2.char* string 与CString

<1>char*->CString

    char *p = "good";
    CString cstr = p;
    //或
    char *p = "good";
    CString cstr;
    cstr.Format("%s",p);

<2>string->CString

    string str = "good";
    CString cstr = str.c_str();
    //或
    string str = "good";
    CString cstr;
    cstr.Format("%s",str.c_str());

<3>CString->char*

    CString str = _T("good");
    char *chr=new char[cstr.GetLength()+1];
    WideCharToMultiByte(CP_ACP,0,str.AllocSysString(),-1,chr,cstr.GetLength()+1,NULL,NULL);
    //或
    char *p;
    int nCharLen;
    nCharLen = WideCharToMultiByte(CP_ACP, 0,str.AllocSysString(), -1, NULL, 0, NULL, NULL);
    p = new char [nCharLen + 1];
    memset(p,0,nCharLen + 1);
    WideCharToMultiByte(CP_ACP, 0, str.AllocSysString(), -1, p, nCharLen + 1, NULL, NULL);

<4>CString->string

    CString cstr = "good";
    string str = cstr;
    //或
    string str = CStringA(cstr);
    //或
    char *chr=new char[cstr.GetLength()+1];
  WideCharToMultiByte(CP_ACP,0,cstr.AllocSysString(),-1,chr,cstr.GetLength()+1,NULL,NULL);
    string str=chr;

3.数字与字符串转化
1.double与string

<1>double->string
    char temp[20];
    double a = 0.631;
    sprintf_s(temp, "%.2f", a);//保留2位小数
    string str =temp;

<2>string->double

    string a = "0.631";
    double b = atof(a.c_str());

2.double与char*
<1>double->char*

    char temp[20];
    double a = 0.631;
    sprintf_s(temp, "%.2f", a);//保留2位小数
    char *p = temp;

<2>char*->double

    char *a = "0.631";
    double b = atof(a);
//如果数字是int型,只需“%f”->"%d"、atof->atoi

3.double与CString
<1>double->CString

    double a = 0.631;
    CString cstr;
    cstr.Format("%.2f",a);

<2>CString->double

    CString cstr = "0.631";
    double a = atof(cstr);  
//如果数字是int型,只需“%f”->"%d"、atof->atoi

4 其它字符串之间的转化
<1> LPCWSTR 转 char*

/***********************************************************************
    函数名称:ConvertLPWSTRToChar
    函数功能:字符串转化,LPCWSTR转化为char*
    函数参数:
            lpwszStrIn  :LPCWSTR字符串

    返回值: 转化结果
    创建人 :pzh
    创建时间:2016.3.4
    备注:
***********************************************************************/
char*  ConvertLPWSTRToChar (LPWSTR lpwszStrIn)  
{  
    LPSTR pszOut = NULL;  
    if (lpwszStrIn != NULL)  
    {  
        int nInputStrLen = wcslen (lpwszStrIn);  

        // Double NULL Termination  
        int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;  
        pszOut = new char [nOutputStrLen];  

        if (pszOut)  
        {  
            memset (pszOut, 0x00, nOutputStrLen);  
            WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);  
        }  
    }  
    return pszOut;  
}  

<2>string 转 LPWSTRT或WCHAR

string strTemp = "113";
WCHAR wcharTemp[256];  //string 转 LPWSTRT
MultiByteToWideChar(CP_ACP,0,strTemp.c_str(),-1,wcharTemp,sizeof(wcharTemp)/sizeof(wcharTemp[0])); 
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值