Tool class Ctools implement character encoding conversion and get current path

Source: Internet
Author: User

classctools{ Public: Ctools (void); Public:    ~ctools (void); Public:    StaticSTD::stringUnicode_to_utf8 (Constcstring&unicodestring); StaticCString Utf8_to_unicode (ConstSTD::string&utf8_string); StaticSTD::stringWs2s (std::wstring&Inputws); StaticStd::wstring S2ws (ConstSTD::string&s); StaticSTD::stringUnicode_to_ansi (Constcstring&unicodestring); StaticCString Ansi_to_unicode (ConstSTD::string&utf8_string); StaticSTD::stringGetFullPath (void);}; Ctools::ctools (void) {}ctools::~ctools (void) {}std::stringCtools::unicode_to_utf8 (Constcstring&unicodestring) {    intStringlength =:: WideCharToMultiByte (Cp_utf8, NULL, UnicodeString, (int) Wcslen (unicodestring), NULL,0, NULL, NULL); Char* Buffer =New Char[Stringlength +1]; :: WideCharToMultiByte (Cp_utf8, NULL, UnicodeString, (int) Wcslen (unicodestring), buffer, stringlength, NULL, NULL); Buffer[stringlength]=' /'; STD::stringstr =buffer; Delete[] buffer; returnstr;} CString Ctools::utf8_to_unicode (ConstSTD::string&utf8_string) {    intLength = (int) utf8_string.size (); intWcsLen =:: MultiByteToWideChar (Cp_utf8, NULL, UTF8_STRING.C_STR (), Length, NULL,0); wchar_t* Wszstring =NewWchar_t[wcslen +1];    :: MultiByteToWideChar (Cp_utf8, NULL, Utf8_string.c_str (), length, wszstring, WcsLen); Wszstring[wcslen]=' /';     CString UnicodeText (wszstring); Delete[] wszstring; returnUnicodeText;} STD::stringCtools::unicode_to_ansi (Constcstring&unicodestring) {    intStringlength =:: WideCharToMultiByte (CP_ACP, NULL, UnicodeString, (int) Wcslen (unicodestring), NULL,0, NULL, NULL); Char* Buffer =New Char[Stringlength +1]; :: WideCharToMultiByte (CP_ACP, NULL, UnicodeString, (int) Wcslen (unicodestring), buffer, stringlength, NULL, NULL); Buffer[stringlength]=' /'; STD::stringstr =buffer; Delete[] buffer; returnstr;} CString Ctools::ansi_to_unicode (ConstSTD::string&utf8_string) {    intLength = (int) utf8_string.size (); intWcsLen =:: MultiByteToWideChar (CP_ACP, NULL, UTF8_STRING.C_STR (), Length, NULL,0); wchar_t* Wszstring =NewWchar_t[wcslen +1];    :: MultiByteToWideChar (CP_ACP, NULL, Utf8_string.c_str (), length, wszstring, WcsLen); Wszstring[wcslen]=' /';     CString UnicodeText (wszstring); Delete[] wszstring; returnUnicodeText;} STD::stringCtools::ws2s (std::wstring&Inputws) {    returnUnicode_to_utf8 (Inputws.c_str ());} Std::wstring Ctools::s2ws (ConstSTD::string&s) {CString CStr=Utf8_to_unicode (s); returnCStr. GetBuffer (CStr. GetLength ());} STD::stringCtools::getfullpath (void) {hmodule h= GetModuleHandle (L"Test.exe"); wchar_t Exefullpath[max_path]; //MAX_PATH is defined in the API and is     intlen=GetModuleFileName (H, Exefullpath,//full path storage address for the applicationMAX_PATH); Std::wstring Wstrpath=Exefullpath; STD::stringstrpath =ctools::ws2s (Wstrpath);     size_t NPos; NPos=strpath.rfind ('\\'); returnSTRPATH.SUBSTR (0, NPos); }

Tool class Ctools implement character encoding conversion and get current path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.