c语言获取windows代码页,Windows下编码转换

#pragma once

#ifndef __HCODEC_HPP__

#define __HCODEC_HPP__

#include

#include

class hCodec {

//不可实例化

hCodec () = delete;

~hCodec () = delete;

static bool hCodec::_conv_Down (std::wstring& _old, std::string& _new, UINT ToType) {

int lenOld = lstrlenW (_old.c_str ());

int lenNew = ::WideCharToMultiByte (ToType, 0, _old.c_str (), lenOld, NULL, 0, NULL, NULL);

std::string s;

s.resize (lenNew);

bool bRet = ::WideCharToMultiByte (ToType, 0, _old.c_str (), lenOld, const_cast(s.c_str ()), lenNew, NULL, NULL);

_new.clear ();

_new = s.c_str ();

return bRet;

}

static bool hCodec::_conv_Up (std::string& _old, std::wstring& _new, UINT ToType) {

int lenOld = lstrlenA (_old.c_str ());

int lenNew = ::MultiByteToWideChar (ToType, 0, _old.c_str (), lenOld, NULL, 0);

std::wstring s;

s.resize (lenNew);

bool bRet = ::MultiByteToWideChar (ToType, 0, _old.c_str (), lenOld, const_cast(s.c_str ()), lenNew);

_new.clear ();

_new = s.c_str ();

return bRet;

}

public:

static bool hCodec::AnsiToUnicode (std::string& _old, std::wstring& _new) {

return hCodec::_conv_Up (_old, _new, CP_ACP);

}

static bool hCodec::UnicodeToAnsi (std::wstring& _old, std::string& _new) {

return hCodec::_conv_Down (_old, _new, CP_ACP);

}

static bool hCodec::Utf8ToUnicode (std::string& _old, std::wstring& _new) {

return hCodec::_conv_Up (_old, _new, CP_UTF8);

}

static bool hCodec::UnicodeToUtf8 (std::wstring& _old, std::string& _new) {

return hCodec::_conv_Down (_old, _new, CP_UTF8);

}

static bool hCodec::AnsiToUtf8 (std::string& _old, std::string& _new) {

std::wstring t;

if (!hCodec::AnsiToUnicode (_old, t)) return false;

return hCodec::UnicodeToUtf8 (t, _new);

}

static bool hCodec::Utf8ToAnsi (std::string& _old, std::string& _new) {

std::wstring t;

if (!hCodec::Utf8ToUnicode (_old, t)) return false;

return hCodec::UnicodeToAnsi (t, _new);

}

};

#endif //__HCODEC_HPP__

#pragma once

#ifndef __HCODEC_HPP__

#define __HCODEC_HPP__

#include

#include

class hCodec {

//不可实例化

hCodec () = delete;

~hCodec () = delete;

static bool hCodec::_conv_Down (std::wstring& _old, std::string& _new, UINT ToType) {

int lenOld = lstrlenW (_old.c_str ());

int lenNew = ::WideCharToMultiByte (ToType, 0, _old.c_str (), lenOld, NULL, 0, NULL, NULL);

std::string s;

s.resize (lenNew);

bool bRet = ::WideCharToMultiByte (ToType, 0, _old.c_str (), lenOld, const_cast(s.c_str ()), lenNew, NULL, NULL);

_new.clear ();

_new = s.c_str ();

return bRet;

}

static bool hCodec::_conv_Up (std::string& _old, std::wstring& _new, UINT ToType) {

int lenOld = lstrlenA (_old.c_str ());

int lenNew = ::MultiByteToWideChar (ToType, 0, _old.c_str (), lenOld, NULL, 0);

std::wstring s;

s.resize (lenNew);

bool bRet = ::MultiByteToWideChar (ToType, 0, _old.c_str (), lenOld, const_cast(s.c_str ()), lenNew);

_new.clear ();

_new = s.c_str ();

return bRet;

}

public:

static bool hCodec::AnsiToUnicode (std::string& _old, std::wstring& _new) {

return hCodec::_conv_Up (_old, _new, CP_ACP);

}

static bool hCodec::UnicodeToAnsi (std::wstring& _old, std::string& _new) {

return hCodec::_conv_Down (_old, _new, CP_ACP);

}

static bool hCodec::Utf8ToUnicode (std::string& _old, std::wstring& _new) {

return hCodec::_conv_Up (_old, _new, CP_UTF8);

}

static bool hCodec::UnicodeToUtf8 (std::wstring& _old, std::string& _new) {

return hCodec::_conv_Down (_old, _new, CP_UTF8);

}

static bool hCodec::AnsiToUtf8 (std::string& _old, std::string& _new) {

std::wstring t;

if (!hCodec::AnsiToUnicode (_old, t)) return false;

return hCodec::UnicodeToUtf8 (t, _new);

}

static bool hCodec::Utf8ToAnsi (std::string& _old, std::string& _new) {

std::wstring t;

if (!hCodec::Utf8ToUnicode (_old, t)) return false;

return hCodec::UnicodeToAnsi (t, _new);

}

};

#endif //__HCODEC_HPP__

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值