很多时候,一些小功能完全可以封装成库供调用,但很多时候网上给出的都是教程,而不是现成可使用的库。这就造成一个问题:实现一些简单的功能也得学大半天,严重影响工期。这儿我直接给出编码转换的源代码以及调用方法,供朋友们使用。
首先是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__
#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);
}
#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,这东西对于字符串处理来说很方便,于是就用它了。