数据类型转换

1、string、CString互换

// string --> CString   
CString.format("%s", string.c_str());   

// CString --> string   
string s(CString.GetBuffer());

GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.  

 2、string、int互换

// string --> int   
_ttoi(str.c_str())

// int --> string
char ch[10];
sprintf(ch, "%d", n);
string s = ch;

3、string、char*互换

有三种方法可以将str转换为char*类型,分别是:data()、 c_str()、 copy()

string-->char*

1)data()方法

string str = "hello";
const char* p = str.data();//加const  或者用char * p=(char*)str.data();的形式

同时有一点需要说明,这里在devc++中编译需要添加const,否则会报错invalid conversion from const char*  to char *,这里可以再前面加上const或者在等号后面给强制转化成char*的类型。

下面解释下该问题,const char*是不能直接赋值到char*的,这样编译都不能通过,理由:假如可以的话,那么通过char*就可以修改const char指向的内容了,这是不允许的。所以char*要另外开辟新的空间,即上面的形式。

2)c_str()方法

string str=“world”;
const char *p = str.c_str();//同上,要加const或者等号右边用char*

3)copy()方法

string str="hmmm";
char p[50];
str.copy(p, 5, 0);//这里5代表复制几个字符,0代表复制的位置,
*(p+5)=‘\0’;//注意手动加结束符!!!

char*-->string

string s;
char *p = "hello";//直接赋值
s = p;

 4、string、wchar_t*

wchar_t*-->string

void WideCharToString(std::string& szDst, wchar_t *wchar)
{
    wchar_t * wText = wchar;
    DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用
    char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
    psText = new char[dwNum];
    WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用
    szDst = psText;// std::string赋值
    delete []psText;// psText的清除
}

5、string、wstring

string-->wstring

std::wstring s2ws(const std::string& s)
{ 
    return Ansi2WChar(s.c_str(),s.size());
} 

std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
{
    int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
    if(nSize <= 0) return NULL;

    WCHAR *pwszDst = new WCHAR[nSize+1];
    if( NULL == pwszDst) return NULL;

    MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
    pwszDst[nSize] = 0;

    if( pwszDst[0] == 0xFEFF) // skip Oxfeff
    {
        for(int i = 0; i < nSize; i ++)
            pwszDst[i] = pwszDst[i+1];
    }

    std::wstring wcharString(pwszDst);
    delete pwszDst;

    return wcharString;
} 

wstring-->string

std::string ws2s(std::wstring& inputws)
{ 
    return WChar2Ansi(inputws.c_str()); 
} 

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;
} 

6、GB2312-->UTF-8

GB2312-->UTF-8

char* G2U(const char* gb2312)
{
    int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
    len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len+1];
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return str;
}

UTF-8-->GB2312

char* U2G(const char* utf8)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len+1];
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return str;
}

 

posted @ 2020-04-03 14:09  zero_to_infinity  阅读(305)  评论(0编辑  收藏  举报