Utils.h
#pragma once
#include <string>
using namespace std;
std::string ConverANSI2UTF8(const std::string & str);
std::wstring ConverANSI2Unicode(const std::string str);
std::wstring ConverUTF82Unicode(const std::string str);
std::string ConverUnicode2UTF8(const std::wstring str);
std::string ConverUnicode2ANSI(const std::wstring str);
std::string ConverUTF82ANSI(const std::string str);
Utils.cpp
#include "utils.h"
using namespace std;
std::wstring ConverANSI2Unicode(const std::string str)
{
int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
std::wstring wstr = pUnicode;
delete[] pUnicode;
return wstr;
}
std::wstring ConverUTF82Unicode(const std::string str)
{
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
std::wstring wstr = pUnicode;
delete[] pUnicode;
return wstr;
}
std::string ConverUnicode2UTF8(const std::wstring wstr)
{
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
char *pUtf8 = new char[utf8Len+1];
memset(pUtf8,0, utf8Len+1);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pUtf8, utf8Len, NULL, NULL);
string str = pUtf8;
delete[] pUtf8;
return str;
}
std::string ConverUnicode2ANSI(const std::wstring wstr)
{
int ansiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
char *pAnsi = new char[ansiLen+1];
memset(pAnsi,0, ansiLen+1);
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAnsi, ansiLen, NULL, NULL);
string str = pAnsi;
delete[] pAnsi;
return str;
}
std::string ConverANSI2UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete []pwBuf;
delete []pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}
std::string ConverUTF82ANSI(const std::string str)
{
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
int ansiLen = WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, NULL, 0, NULL, NULL);
char *pAnsi = new char[ansiLen+1];
memset(pAnsi,0, ansiLen+1);
WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsi, ansiLen, NULL, NULL);
string str1 = pAnsi;
delete[] pUnicode;
delete[] pAnsi;
return str1;
}
CString GetModulePath()
{
CString ModulePath;
wchar_t FileName[MAX_PATH];
GetModuleFileName(NULL,FileName,MAX_PATH);
ModulePath.Append(FileName);
int loc=ModulePath.ReverseFind('\\');
ModulePath=ModulePath.Left(loc);
return ModulePath;
}
int LanguageTypeByXml(CString dllPath,CString fileName)
{
CMarkup xml;
CString xmlfileName = dllPath +fileName;
xml.Load(xmlfileName);
if (xml.FindChildElem(L"appSettings"))
{
xml.IntoElem();
while(xml.FindChildElem(L"add"))
{
CString key;
key=xml.GetChildAttrib(L"key");
if(key.CompareNoCase(L"Language")==0)
{
CString DefaultLanguage=xml.GetChildAttrib(L"value");
if(DefaultLanguage.CompareNoCase(L"zh-CN") == 0)
return 0;
if(DefaultLanguage.CompareNoCase(L"en-US") == 0)
return 1;
}
}
}/**/
return 0; //默认中文版
}
map<wstring, wstring> GetLanguageTrans(int LanguageType)
{
map<wstring, wstring> langMap;
if(LanguageType == 0) //中文
{
langMap[L"IDS_STRING200"] = L"完成写入证据库";
langMap[L"IDS_STRING201"] = L"正在写入证据库...";
langMap[L"IDS_STRING202"] = L"完成数据汇总";
langMap[L"IDS_STRING203"] = L"正在数据汇总...";
}
else //英文
{
langMap[L"IDS_STRING200"] = L"Complete Writing To The Evidence Database";
langMap[L"IDS_STRING201"] = L"Writing To The Evidence Database...";
langMap[L"IDS_STRING202"] = L"Complete data summary";
langMap[L"IDS_STRING203"] = L"Summary data...";
}
return langMap;
}
这个代码段展示了在C++中进行ANSI、UTF8和Unicode编码间的转换函数,以及获取模块路径的方法。转换函数使用Windows API `MultiByteToWideChar` 和 `WideCharToMultiByte`,而`GetModulePath`函数则用于获取当前模块的路径。

1029

被折叠的 条评论
为什么被折叠?



