码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 常用转换API记录

时间:2021-01-11 10:29:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:att   c_str   std   char   nbsp   chart   ng2   ace   编码   

//wstring转string
std::string wstring2string(IN std::wstring& wstr)
{
    std::string result;
    //获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
    int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
    char* buffer = new char[len + 1];
    //宽字节编码转换成多字节编码
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
    buffer[len] = \0;
    //删除缓冲区并返回值  
    result.append(buffer);
    delete[] buffer;
    return result;
}

//string转wstring
std::wstring string2wstring(IN std::string str)
{
    std::wstring result;
    //获取缓冲区大小,并申请空间,缓冲区大小按字符计算  
    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
    TCHAR* buffer = new TCHAR[len + 1];
    //多字节编码转换成宽字节编码  
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
    buffer[len] = \0;//添加字符串结尾  
    //删除缓冲区并返回值  
    result.append(buffer);
    delete[] buffer;
    return result;
}

//CstringToString
std::string cstring2string(IN CString cstr)
{
    //ATL字符串转换宏
    std::string str = CT2A(cstr.GetString());
    return str;
}

//StringToCstring
CString string2cstring(IN std::string str)
{
    //ATL字符串转换宏
    CString cc = CA2T(str.c_str());
    return cc;
}

//UTF-8到GB2312的转换
std::string U2G(IN const char* utf8)
{
    std::string result;
    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);
    result.append(str);
    if (wstr) delete[] wstr;
    if (str) delete[] str;
    return result;
}

//GB2312到UTF-8的转换
std::string G2U(IN const char* gb2312)
{
    std::string result;
    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);
    result.append(str);
    if (wstr) delete[] wstr;
    if (str) delete[] str;
    return result;
}

//字符串替换
std::string& replace_str(IN std::string& str, IN  const std::string& pattern, IN const std::string& replacestr)
{
    for (std::string::size_type pos(0); pos != std::string::npos; pos += replacestr.length())
    {
        pos = str.find(pattern, pos);
        if (pos != std::string::npos)
            str.replace(pos, pattern.length(), replacestr);
        else
            break;
    }
    return str;
}

 参考链接:

https://www.cnblogs.com/babietongtianta/p/3143900.html

https://www.cnblogs.com/lpxblog/p/9855791.html

C++ 常用转换API记录

标签:att   c_str   std   char   nbsp   chart   ng2   ace   编码   

原文地址:https://www.cnblogs.com/rcg714786690/p/14246554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!