utf8-unicode(stl string版)

void UCS2_TO_UTF8(std::string &dst, const std::wstring &src)
{
    size_t l, i;

    l = src.length();
    for (i = 0; i < l; i++)
    {
        wchar_t t;

        t = src[i];
        if (t <= 0x7F)
        {
            dst.push_back(t & 0x7F);
        }
        else if (t <= 0x7FF)
        {
            dst.push_back((6 << 5) | (t >> 11));
            dst.push_back((2 << 6) | (t & 0x3F));
        }
        else
        {
            dst.push_back((14 << 4) | (t >> 12));
            dst.push_back((2 << 6)  | ((t >> 6) & 0x3F));
            dst.push_back((2 << 6)  | (t & 0x3F));
        }
    }
    return;
}

void UTF8_TO_UCS2(std::wstring &dst, const std::string &src)
{
    size_t l, i;

    i = 0;
    l = src.length();
    while (i < l)
    {
        char t;
        long n;

        n = 0;
        t = src[i];
        while (t & 0x80)
        {
            n++;
            t = t << 1;
        }
        switch (n)
        {
        case 0:
            {
                dst.push_back(src[i] & 0x7F);
                i += 1;
            }
            break;
        case 2:
            {
                if (i + 1 >= l)
                    return;
                dst.push_back(((src[i] & 0x1F) << 6) | (src[i + 1] & 0x3F));
                i += 2;
            }
            break;
        case 3:
            {
                if (i + 2 >= l)
                    return;
                dst.push_back(((src[i] & 0x1F) << 12) | ((src[i + 1] & 0x3F) << 6) | (src[i + 2] & 0x3F));
                i += 3;
            }
            break;
        default:
            return;
        }
    }
    return;
}

你可能感兴趣的:(utf8-unicode(stl string版))