Windows下C++实现编码转换(SDK、MFC)

很多时候,一些小功能完全可以封装成库供调用,但很多时候网上给出的都是教程,而不是现成可使用的库。这就造成一个问题:实现一些简单的功能也得学大半天,严重影响工期。这儿我直接给出编码转换的源代码以及调用方法,供朋友们使用。

首先是hCodec.h文件:

SDK版:

#ifndef __H_CODEC_H__
#define __H_CODEC_H__

#pragma once

#include <string>

class hCodec {
	hCodec ();//不可实例化
	//私有方法
	static bool _conv_Down (std::wstring& _old, std::string& _new, UINT ToType);
	static bool _conv_Up (std::string& _old, std::wstring& _new, UINT ToType);
public:
	static bool AnsiToUnicode (std::string& _old, std::wstring& _new);
	static bool UnicodeToAnsi (std::wstring& _old, std::string& _new);
	static bool Utf8ToUnicode (std::string& _old, std::wstring& _new);
	static bool UnicodeToUtf8 (std::wstring& _old, std::string& _new);
	static bool AnsiToUtf8 (std::string& _old, std::string& _new);
	static bool Utf8ToAnsi (std::string& _old, std::string& _new);
};

#endif //__H_CODEC_H__


MFC版:
#ifndef __H_CODEC_H__
#define __H_CODEC_H__

#pragma once

#include <afxstr.h>

class hCodec {
	hCodec ();//不可实例化
	//私有方法
	static bool _conv_Down (CStringW& _old, CStringA& _new, UINT ToType);
	static bool _conv_Up (CStringA& _old, CStringW& _new, UINT ToType);
public:
	static bool AnsiToUnicode (CStringA& _old, CStringW& _new);
	static bool UnicodeToAnsi (CStringW& _old, CStringA& _new);
	static bool Utf8ToUnicode (CStringA& _old, CStringW& _new);
	static bool UnicodeToUtf8 (CStringW& _old, CStringA& _new);
	static bool AnsiToUtf8 (CStringA& _old, CStringA& _new);
	static bool Utf8ToAnsi (CStringA& _old, CStringA& _new);
};

#endif //__H_CODEC_H__


然后是hCodec.cpp文件:

SDK版:

#include "hCodec.h"
#include <Windows.h>



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<char*>(s.c_str ()), lenNew, NULL, NULL);
	_new.clear ();
	_new = s.c_str ();
	return bRet;
}
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<wchar_t*>(s.c_str ()), lenNew);
	_new.clear ();
	_new = s.c_str ();
	return bRet;
}

bool hCodec::AnsiToUnicode (std::string& _old, std::wstring& _new) {
	return hCodec::_conv_Up (_old, _new, CP_ACP);
}
bool hCodec::UnicodeToAnsi (std::wstring& _old, std::string& _new) {
	return hCodec::_conv_Down (_old, _new, CP_ACP);
}
bool hCodec::Utf8ToUnicode (std::string& _old, std::wstring& _new) {
	return hCodec::_conv_Up (_old, _new, CP_UTF8);
}
bool hCodec::UnicodeToUtf8 (std::wstring& _old, std::string& _new) {
	return hCodec::_conv_Down (_old, _new, CP_UTF8);
}
bool hCodec::AnsiToUtf8 (std::string& _old, std::string& _new) {
	std::wstring t;
	if (!hCodec::AnsiToUnicode (_old, t)) return false;
	return hCodec::UnicodeToUtf8 (t, _new);
}
bool hCodec::Utf8ToAnsi (std::string& _old, std::string& _new) {
	std::wstring t;
	if (!hCodec::Utf8ToUnicode (_old, t)) return false;
	return hCodec::UnicodeToAnsi (t, _new);
}


MFC版:
#include "stdafx.h"
#include "hCodec.h"



bool hCodec::_conv_Down (CStringW& _old, CStringA& _new, UINT ToType) {
	int lenOld = lstrlenW (_old.GetBuffer ());
	int lenNew = ::WideCharToMultiByte (ToType, 0, _old.GetBuffer (), lenOld, NULL, 0, NULL, NULL);
	CStringA s;
	s.GetBufferSetLength (lenNew);
	bool bRet = ::WideCharToMultiByte (ToType, 0, _old.GetBuffer (), lenOld, const_cast<char*>(s.GetBuffer ()), lenNew, NULL, NULL);
	_new = s.GetBuffer ();
	return bRet;
}
bool hCodec::_conv_Up (CStringA& _old, CStringW& _new, UINT ToType) {
	int lenOld = lstrlenA (_old.GetBuffer ());
	int lenNew = ::MultiByteToWideChar (ToType, 0, _old.GetBuffer (), lenOld, NULL, 0);
	CStringW s;
	s.GetBufferSetLength (lenNew);
	bool bRet = ::MultiByteToWideChar (ToType, 0, _old.GetBuffer (), lenOld, const_cast<wchar_t*>(s.GetBuffer ()), lenNew);
	_new = s.GetBuffer ();
	return bRet;
}

bool hCodec::AnsiToUnicode (CStringA& _old, CStringW& _new) {
	return hCodec::_conv_Up (_old, _new, CP_ACP);
}
bool hCodec::UnicodeToAnsi (CStringW& _old, CStringA& _new) {
	return hCodec::_conv_Down (_old, _new, CP_ACP);
}
bool hCodec::Utf8ToUnicode (CStringA& _old, CStringW& _new) {
	return hCodec::_conv_Up (_old, _new, CP_UTF8);
}
bool hCodec::UnicodeToUtf8 (CStringW& _old, CStringA& _new) {
	return hCodec::_conv_Down (_old, _new, CP_UTF8);
}
bool hCodec::AnsiToUtf8 (CStringA& _old, CStringA& _new) {
	CStringW t;
	if (!hCodec::AnsiToUnicode (_old, t)) return false;
	return hCodec::UnicodeToUtf8 (t, _new);
}
bool hCodec::Utf8ToAnsi (CStringA& _old, CStringA& _new) {
	CStringW t;
	if (!hCodec::Utf8ToUnicode (_old, t)) return false;
	return hCodec::UnicodeToAnsi (t, _new);
}



代码贴完了,接下来是如何调用。调用方法都差不多,我只贴一个关于SDK版的使用,其他编码转换的代码都差不多。

//gb2312编码到unicode编码的转换
std::string ansi = "hello world!";
std::wstring unicode;
hCodec::AnsiToUnicode (ansi, unicode);
//然后unicode这儿就是转换后的文本了

调用方法还是多方便的。我简单说说代码的逻辑吧。主要通过调用 Win32 API    WideCharToMultiByte 和 MultiByteToWideChar 这两货实现编码转换。另外程序中使用到了C艹的std::string,这东西对于字符串处理来说很方便,于是就用它了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值