ASCII Unicode UTF-8互转

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <vector>

 

//Unicode  ANSCII
std::string Unicode2Ascii(const std::wstring& mStrIn)
{
    int mAsciiSize = ::WideCharToMultiByte(CP_ACP, 0, mStrIn.c_str(), -1, NULL, 0, NULL, NULL);
    if (mAsciiSize == ERROR_NO_UNICODE_TRANSLATION)
    {
        throw std::exception("Unicode2Anscii Error COde : ERROR_NO_UNICODE_TRANSLATION");
    }
    if (mAsciiSize == 0)
    {
        throw std::exception("Unicode2Anscii : Error in conversion.");
    }
    std::vector<char> mOut(mAsciiSize);

    if (::WideCharToMultiByte(CP_ACP, 0, mStrIn.c_str(), -1, &mOut[0], mAsciiSize, NULL, NULL) != mAsciiSize)
    {
        throw std::exception("Unicode2Anscii :  Not the Same!");
    }
    return std::string(&mOut[0]);
}

//UTF8  Unicode
std::wstring UTF82Unicode(const std::string& mIn)
{
    int mSize = ::MultiByteToWideChar(CP_UTF8, 0, mIn.c_str(), -1, NULL, 0);
    if (mSize == ERROR_NO_UNICODE_TRANSLATION)
    {
        throw std::exception("Utf82Unicode Error Code : ERROR_NO_UNICODE_TRANSLATION");
    }

    if (mSize == 0)
    {
        throw std::exception("Utf82Unicode : Error in conversion.");
    }

    std::vector<wchar_t> mOut(mSize);

    if (::MultiByteToWideChar(CP_UTF8, 0, mIn.c_str(), -1, &mOut[0], mSize) != mSize)
    {
        throw std::exception("Utf82Unicode : Not the Same!");
    }

    return std::wstring(&mOut[0]);
}


//UTF8  Ascii
std::string UTF82Ascii(const std::string& mIn)
{
    return Unicode2Ascii(UTF82Unicode(mIn));
}

//ANSCII 转 Unicode 
std::wstring Ascii2Unicode(const std::string& mStrIn)
{
    int mUnicodeSize = MultiByteToWideChar(CP_ACP, 0, (char*)mStrIn.c_str(), -1, NULL, 0);
    if (mUnicodeSize == ERROR_NO_UNICODE_TRANSLATION)
    {
        throw std::exception("Anscii2Unicode Error Code : ERROR_NO_UNICODE_TRANSLATION");
    }
    if (mUnicodeSize == 0)
    {
        throw std::exception("Anscii2Unicode : Error in conversion.");
    }
    std::vector<wchar_t> mOut(mUnicodeSize);

    if (MultiByteToWideChar(CP_ACP, 0, mStrIn.c_str(), -1, &mOut[0], mUnicodeSize) != mUnicodeSize)
    {
        throw std::exception("Anscii2Unicode : Not the Same!");
    }

    return std::wstring(&mOut[0]);
}

//Unicode  UTF8 
std::string Unicode2Utf8(const std::wstring& mIn)
{
    int mSize = ::WideCharToMultiByte(CP_UTF8, 0, mIn.c_str(), -1, NULL, 0, NULL, NULL);
    if (mSize == 0)
    {
        throw std::exception("Unicode2Utf8 : Error in conversion.");
    }

    std::vector<char> mOut(mSize);

    if (::WideCharToMultiByte(CP_UTF8, 0, mIn.c_str(), -1, &mOut[0], mSize, NULL, NULL) != mSize)
    {
        throw std::exception("Unicode2Utf8 : Not the Same!");
    }

    return std::string(&mOut[0]);
}

//Ascii UTF8   
std::string Ascii2UTF8(const std::string& mIn)
{
    return Unicode2Utf8(Ascii2Unicode(mIn));
}

std::vector<std::string> StringSplit(std::string mFind, std::string mSplit)
{
    std::string::size_type mPos;
    std::vector<std::string> mResult;
    mFind += mSplit;
    int size = mFind.size();

    for (int i = 0; i < size; i++)
    {
        mPos = mFind.find(mSplit, i);
        if (mPos < size)
        {
            std::string s = mFind.substr(i, mPos - i);
            mResult.push_back(s);
            i = mPos + mSplit.size() - 1;
        }
    }
    return mResult;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值