宽字节UTF-8多字节互转

Posted wanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了宽字节UTF-8多字节互转相关的知识,希望对你有一定的参考价值。

  在进行Windows编程时,常常遇到不同字符编码之间的转换以对应不同的输出格式,本文介绍宽字节UTF-8编码格式和多字节之间的项目转换。分别调用Windows底层函数MultiByteToWideChar和  WideCharToMultiByte实现。

1.UTF-8转多字节

  

std::string u82mb(const char* cont)
{
    if (NULL == cont)
    {
        return "";
    }

    int num = MultiByteToWideChar(CP_UTF8, NULL, cont, -1, NULL, NULL);
    if (num <= 0)
    {
        return "";
    }
    wchar_t* buffw = new (std::nothrow) wchar_t[num];
    if (NULL == buffw)
    {
        return "";
    }
    MultiByteToWideChar(CP_UTF8, NULL, cont, -1, buffw, num);
    int len = WideCharToMultiByte(CP_ACP, 0, buffw, num - 1, NULL, NULL, NULL, NULL);
    if (len <= 0)
    {
        delete[] buffw;
        return "";
    }
    char* lpsz = new (std::nothrow) char[len + 1]; 
    if (NULL == lpsz)
    {
        delete[] buffw;
        return "";
    }
    WideCharToMultiByte(CP_ACP, 0, buffw, num - 1, lpsz, len, NULL, NULL);
    lpsz[len]=;
    delete[] buffw;
    std::string rtn(lpsz);
    delete[] lpsz;
    return rtn;
}

2.多字节转UTF-8

std::string mb2u8(const char* cont)
{
    if (NULL == cont)
    {
        return "";
    }
    int num = MultiByteToWideChar(CP_ACP, NULL, cont, -1, NULL, NULL);
    if (num <= 0)
    {
        return "";
    }
    wchar_t* buffw = new (std::nothrow) wchar_t[num];
    if (NULL == buffw)
    {
        return "";
    }
    MultiByteToWideChar(CP_ACP, NULL, cont, -1, buffw, num);
    int len = WideCharToMultiByte(CP_UTF8, 0, buffw, num - 1, NULL, NULL, NULL, NULL); 
    if (len <= 0)
    {
        delete[] buffw;
        return "";
    }
    char* lpsz = new (std::nothrow) char[len + 1]; 
    if (NULL == lpsz)
    {
        delete[] buffw;
        return "";
    }
    WideCharToMultiByte(CP_UTF8, 0, buffw, num - 1, lpsz, len, NULL, NULL);
    lpsz[len]=;
    delete[] buffw;
    std::string rtn(lpsz);
    delete[] lpsz;
    return rtn ;
}

 

以上是关于宽字节UTF-8多字节互转的主要内容,如果未能解决你的问题,请参考以下文章

宽字符到多字节问题

宽字节注入

windows字符集

宽字节与多字节互相转换

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]

wchar_t* 和char* 互转