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,這東西對於字符串處理來說很方便,於是就用它了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章