C++ 常用转换API记录

2021-06-10 18:04

阅读:655

标签: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


评论


亲,登录后才可以留言!