关于ANSI和Unicode、Unicode和UTF-8等的相互转换

来源:互联网 发布:魔兽传输数据遇到问题 编辑:程序博客网 时间:2024/04/29 08:08

关于ANSI和Unicode、Unicode和UTF-8等的相互转换可通用的代码如下:

qp::StringW Global::AnsiToUnicode(const charbuf)
{
    int len ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
    if (len == 0) return L"";
 
    std::vector<</CODE>wchar_tunicode(len);
    ::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
 
    return &unicode[0];
}
 
qp::StringA Global::UnicodeToAnsi(const wchar_tbuf)
{
    int len ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
    if (len == 0) return "";
 
    std::vector<</CODE>charutf8(len);
    ::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
 
    return &utf8[0];
}
 
qp::StringW Global::Utf8ToUnicode(const charbuf)
{
    int len ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
    if (len == 0) return L"";
 
    std::vector<</CODE>wchar_tunicode(len);
    ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
 
    return &unicode[0];
}
 
qp::StringA Global::UnicodeToUtf8(const wchar_tbuf)
{
    int len ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
    if (len == 0) return "";
 
    std::vector<</CODE>charutf8(len);
    ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
 
    return &utf8[0];
}
原创粉丝点击