文本的编码转换ANSI、UNICODE、UTF8

文本的编码转换ANSI、UNICODE、UTF8
Windows平台下的编码转换类
ANSI和UNICODE和UTF8的互相转换

Charset.h

#pragma once
#include <tchar.h>
#include <stringapiset.h>
#include <stdlib.h>
#include <string>
class Charset
{
public:
    static std::wstring ANSIToUnicode(const char* ansi)
    {
        int textlen = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
        wchar_t * pBuffer = new wchar_t[textlen + 1];
        memset(pBuffer, 0, (textlen + 1) * sizeof(wchar_t));
        MultiByteToWideChar(CP_ACP, 0, ansi, -1, pBuffer, textlen);
        std::wstring unicode(pBuffer);
        delete[] pBuffer;
        return unicode;
    }

    static BOOL ANSIToUnicode(wchar_t* dst, const int size, const char* ansi)
    {
        BOOL bResult{ FALSE };
        int textlen = 0;
        textlen = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
        if (size > textlen)
        {
            // memset(dst, 0, (textlen + 1) * sizeof(wchar_t));
            if (0 != MultiByteToWideChar(CP_ACP, 0, ansi, -1, dst, textlen))
            {
                bResult = TRUE;
            }
            dst[textlen] = 0;
        }
        return bResult;
    }

    static std::string UnicodeToANSI(const wchar_t *unicode)
    {
        int textlen = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL);
        char * pBuffer = new char[textlen + 1];
        memset(pBuffer, 0, sizeof(char) * (textlen + 1));
        WideCharToMultiByte(CP_ACP, 0, unicode, -1, pBuffer, textlen, NULL, NULL);
        std::string ansi{ pBuffer };
        delete[] pBuffer;
        return ansi;
    }

    static BOOL UnicodeToANSI(char* dst, const int size, const wchar_t *unicode)
    {
        BOOL bResult{ FALSE };
        int textlen = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL);
        if (size > textlen)
        {
            //memset(dst, 0, sizeof(char) * (textlen + 1));
            if (0 != WideCharToMultiByte(CP_ACP, 0, unicode, -1, dst, textlen, NULL, NULL))
            {
                bResult = TRUE;
            }
            dst[textlen] = 0;
        }
        return bResult;
    }

    static std::wstring UTF8ToUnicode(const char* utf8)
    {
        int textlen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
        wchar_t * pBuffer = new wchar_t[textlen + 1];
        memset(pBuffer, 0, (textlen + 1) * sizeof(wchar_t));
        MultiByteToWideChar(CP_UTF8, 0, utf8, -1, (LPWSTR)pBuffer, textlen);
        std::wstring unicode{ pBuffer };
        delete[]pBuffer;
        return unicode;
    }

    static std::wstring UTF8ToUnicode(const wchar_t* utf8)
    {
        std::string u = UnicodeToANSI(utf8);
        int textlen = MultiByteToWideChar(CP_UTF8, 0, u.c_str(), -1, NULL, 0);
        wchar_t * pBuffer = new wchar_t[textlen + 1];
        memset(pBuffer, 0, (textlen + 1) * sizeof(wchar_t));
        MultiByteToWideChar(CP_UTF8, 0, u.c_str(), -1, (LPWSTR)pBuffer, textlen);
        std::wstring unicode{ pBuffer };
        delete[]pBuffer;
        return unicode;
    }

    static BOOL UTF8ToUnicode(wchar_t*dst, const int size, const char* utf8)
    {
        BOOL bResult{ FALSE };
        int textlen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
        if (size > textlen)
        {
            // memset(dst, 0, (textlen + 1) * sizeof(wchar_t));
            if (0 != MultiByteToWideChar(CP_UTF8, 0, utf8, -1, (LPWSTR)dst, textlen))
            {
                bResult = TRUE;
            }
            dst[textlen] = 0;
        }
        return bResult;
    }

    static std::string UnicodeToUTF8(const wchar_t *unicode)
    {
        int textlen = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
        char * pBuffer = new char[textlen + 1];
        memset(pBuffer, 0, sizeof(char) * (textlen + 1));
        WideCharToMultiByte(CP_UTF8, 0, unicode, -1, pBuffer, textlen, NULL, NULL);
        std::string utf8{ pBuffer };
        delete[] pBuffer;
        return utf8;
    }

    static BOOL UnicodeToUTF8(char* dst, const int size, const wchar_t *unicode)
    {
        BOOL bResult{ FALSE };
        int textlen = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
        if (size > textlen)
        {
            // memset(dst, 0, sizeof(char) * (textlen + 1));
            if (0 != WideCharToMultiByte(CP_UTF8, 0, unicode, -1, dst, textlen, NULL, NULL))
            {
                bResult = TRUE;
            }
            dst[textlen] = 0;
        }
        return bResult;
    }
    // char utf8 -> unicode -> ansi
    static std::string UTF8ToANSI(const char* utf8)
    {
        std::wstring unicode = UTF8ToUnicode(utf8);
        std::string ansi = UnicodeToANSI(unicode.c_str());
        return ansi;
    }

#ifdef _UNICODE
    // wchar utf8 -> char utf8 -> unicode
    static std::wstring UTF8ToLocal(const wchar_t* utf8)
    {
        std::wstring unicode = UTF8ToUnicode(utf8);
        return unicode;
    }
#else
    // char utf8 -> unicode -> ansi
    static std::string UTF8ToLocal(const char* utf8)
    {
        std::string ansi = UTF8ToANSI(utf8);
        return ansi;
    }
#endif
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值