1 /*! 2 @file 3 @author Albert Semenov 4 @date 01/2009 5 @module 6 */ 7 8 #include <MyGUI.h> 9 #include "Utility.h" 10 #include <windows.h> 11 12 namespace string_utility 13 { 14 utf16_to_utf8(const std::wstring & _source)15 std::string utf16_to_utf8(const std::wstring& _source) 16 { 17 const wchar_t* srcPtr = _source.c_str(); 18 int dstSize = WideCharToMultiByte( CP_UTF8, 0, srcPtr, (int)_source.size(), 0, 0, 0, 0 ); 19 char* dest = new char [ dstSize + 1 ]; 20 WideCharToMultiByte( CP_UTF8, 0, srcPtr, (int)_source.size(), dest, dstSize, 0, 0 ); 21 dest[dstSize] = 0; 22 std::string ret = dest; 23 delete [] dest; 24 return ret; 25 } 26 utf16_to_ansi(const std::wstring & _source)27 std::string utf16_to_ansi(const std::wstring& _source) 28 { 29 const wchar_t* srcPtr = _source.c_str(); 30 int dstSize = WideCharToMultiByte( CP_ACP, 0, srcPtr, (int)_source.size(), 0, 0, 0, 0 ); 31 char* dest = new char [ dstSize + 1 ]; 32 WideCharToMultiByte( CP_ACP, 0, srcPtr, (int)_source.size(), dest, dstSize, 0, 0 ); 33 dest[dstSize] = 0; 34 std::string ret = dest; 35 delete [] dest; 36 return ret; 37 } 38 utf8_to_utf16(const std::string & _source)39 std::wstring utf8_to_utf16(const std::string& _source) 40 { 41 const char* srcPtr = _source.c_str(); 42 int tmpSize = MultiByteToWideChar( CP_UTF8, 0, srcPtr, -1, 0, 0 ); 43 WCHAR* tmpBuff = new WCHAR [ tmpSize + 1 ]; 44 MultiByteToWideChar( CP_UTF8, 0, srcPtr, -1, tmpBuff, tmpSize ); 45 std::wstring ret = tmpBuff; 46 delete[] tmpBuff; 47 return ret; 48 } 49 ansi_to_utf16(const std::string & _source)50 std::wstring ansi_to_utf16(const std::string& _source) 51 { 52 const char* srcPtr = _source.c_str(); 53 int tmpSize = MultiByteToWideChar( CP_ACP, 0, srcPtr, -1, 0, 0 ); 54 WCHAR* tmpBuff = new WCHAR [ tmpSize + 1 ]; 55 MultiByteToWideChar( CP_ACP, 0, srcPtr, -1, tmpBuff, tmpSize ); 56 std::wstring ret = tmpBuff; 57 delete[] tmpBuff; 58 return ret; 59 } 60 61 } // namespace string_utility 62