Add character encode/decode functions

This commit is contained in:
Kirigaya Kazuto 2017-09-26 23:02:31 +08:00
parent cced27fc53
commit 386c4633e7
2 changed files with 56 additions and 0 deletions

View File

@ -34,3 +34,15 @@ bool canread(std::string Path);
bool canwrite(std::string Path);
bool isexist(std::string Path);
bool canexecute(std::string Path);
/// Return Value
/// true: transfer succeed. Value is outputed to _out_wstr
/// false: transfer failed. _out_wstr is not modified.
bool _ANSIToUnicode(const std::string& str,std::wstring& _out_wstr);
bool _UTF8ToUnicode(const std::string& str,std::wstring& _out_wstr);
/// Return Value
/// true: transfer succeed. Value is outputed to _out_str
/// false: transfer failed. _out_str is not modified.
bool _UnicodeToANSI(const std::wstring& wstr,std::string& _out_str);
bool _UnicodeToUTF8(const std::wstring& wstr,std::string& _out_str);

View File

@ -84,6 +84,50 @@ string GBKToUTF8(string GBKString)
return s;
}
bool _ANSIToUnicode(const string& str,wstring& _out_wstr)
{
int unicodeLen=MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0);
unique_ptr<wchar_t[]> pUnicode(new wchar_t[unicodeLen+1]);
if(!pUnicode.get()) { return false; }
memset(pUnicode.get(),0,sizeof(wchar_t)*(unicodeLen+1));
MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,pUnicode.get(),unicodeLen);
_out_wstr=wstring(pUnicode.get());
return true;
}
bool _UnicodeToANSI(const wstring& str,string& _out_str)
{
int iTextLen=WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL);
unique_ptr<char[]> pElementText(new char[iTextLen+1]);
if(!pElementText.get()) { return false; }
memset(pElementText.get(),0,sizeof(char)*(iTextLen+1));
WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,pElementText.get(),iTextLen,NULL,NULL);
_out_str=string(pElementText.get());
return true;
}
bool _UTF8ToUnicode(const string& str,wstring& _out_wstr)
{
int unicodeLen=MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
unique_ptr<wchar_t[]> pUnicode(new wchar_t[unicodeLen+1]);
if(!pUnicode.get()) { return false; }
memset(pUnicode.get(),0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,pUnicode.get(),unicodeLen);
_out_wstr=wstring(pUnicode.get());
return true;
}
bool _UnicodeToUTF8(const wstring& str,string& _out_str)
{
int iTextLen=WideCharToMultiByte(CP_UTF8,0,str.c_str(),-1,NULL,0,NULL,NULL);
unique_ptr<char[]> pElementText(new char[iTextLen+1]);
if(!pElementText.get()) { return false; }
memset(pElementText.get(),0,sizeof(char)*(iTextLen+1));
WideCharToMultiByte(CP_UTF8,0,str.c_str(),-1,pElementText.get(),iTextLen,NULL,NULL);
_out_str=string(pElementText.get());
return true;
}
#ifdef _MSC_VER
bool isexist(std::string Path)
{