//UTF-8转Unicode std::w
String Utf82Unicode(const std:
:string& utf8
String)
{ int wIDesize = ::MultiBytetoWIDeChar(CP_UTF8,utf8
String.c_str(),-1,NulL,0
); if (wIDesize == ERROR_NO_UNICODE_TRANSLATION)
{ throw std::exception("InvalID UTF-8 sequence."
); } if (wIDesize == 0)
{ throw std::exception("Error in conversion."
); } std::vector<wchar_t> result
String(wIDesiz
E); int convresult = ::MultiBytetoWIDeChar(CP_UTF8,&result
String
[0],wIDesiz
E); if (convresult
!= wIDesiz
E) { throw std::exception("La
falla!"
); } return std::w
String(&result
String
[0]); } //unicode 转为 ascii
String WIDeByte2Acsi(w
String& wstrcod
E) { int ascIISize = ::WIDeCharToMultiByte(CP_OEMCP,wstrcode.c_str(),
null); if (ascIISize == ERROR_NO_UNICODE_TRANSLATION)
{ throw std::exception("InvalID UTF-8 sequence."
); } if (ascIISize == 0)
{ throw std::exception("Error in conversion."
); } std::vector<char> result
String(ascIISiz
E); int convresult =::WIDeCharToMultiByte(CP_OEMCP,ascIISize,
null); if (convresult
!= ascIISiz
E) { throw std::exception("La
falla!"
); } return std:
:string(&result
String
[0]); } //utf-8 转 ascii
String UTF_82ASCII(
String& strUtf8Cod
E) { String strRet(""
); //先把 utf8 转为 unicode w
String wstr = Utf82Unicode(strUtf8Cod
E); //最后把 unicode 转为 ascii strRet = WIDeByte2Acsi(wstr
); return strRet; } /////////////////////////////////////////////////////////////////////// //ascii 转 Unicode w
String Acsi2WIDeByte(
String& strascii)
{ int wIDesize = MultiBytetoWIDeChar (CP_ACP,(char*)strascii.c_str(),0
); if (wIDesize == ERROR_NO_UNICODE_TRANSLATION)
{ throw std::exception("InvalID UTF-8 sequence."
); } if (wIDesize == 0)
{ throw std::exception("Error in conversion."
); } std::vector<wchar_t> result
String(wIDesiz
E); int convresult = MultiBytetoWIDeChar (CP_ACP,wIDesiz
E); if (convresult
!= wIDesiz
E) { throw std::exception("La
falla!"
); } return std::w
String(&result
String
[0]); } //Unicode 转 Utf8 std:
:string Unicode2Utf8(const std::w
String& wIDe
String)
{ int utf8size = ::WIDeCharToMultiByte(CP_UTF8,wIDe
String.c_str(),
null); if (utf8size == 0)
{ throw std::exception("Error in conversion."
); } std::vector<char> result
String(utf8siz
E); int convresult = ::WIDeCharToMultiByte(CP_UTF8,utf8size,
null); if (convresult
!= utf8siz
E) { throw std::exception("La
falla!"
); } return std:
:string(&result
String
[0]); } //ascii 转 Utf8
String ASCII2UTF_8(
String& strAsciiCod
E) { String strRet(""
); //先把 ascii 转为 unicode w
String wstr = Acsi2WIDeByte(strAsciiCod
E); //最后把 unicode 转为 utf8 strRet = Unicode2Utf8(wstr
); return strRet; }