Standard encoding and decoding functions
Encoding, current default encoding goto UTF-8
BOOL CodeToUtf8 (const std::string &SSRC, std::string &sdst)
{
int ineed = MultiByteToWideChar (CP_ACP, 0, Ssrc.c_str (),-1, NULL, 0);
if (0 = = ineed)
{
WriteToLog ("Util::codetoutf8:multibytetowidechar = = 0, errorcode=%d, ssrc=%s", GetLastError (), SSRC.C_STR ());
return false;
}
Std::wstring swstr (ineed, 0);
MultiByteToWideChar (CP_ACP, 0, Ssrc.c_str (),-1, &swstr[0], swstr.size ());
Ineed = WideCharToMultiByte (Cp_utf8, 0, Swstr.c_str (),-1, NULL, 0, NULL, NULL);
if (0 = = ineed)
{
WriteToLog ("Util::codetoutf8:widechartomultibyte = = 0, errorcode=%d", GetLastError ());
return false;
}
Sdst.resize (ineed, 0);
WideCharToMultiByte (Cp_utf8, 0, Swstr.c_str (),-1, &sdst[0], sdst.size (), NULL, NULL);
return true;
}
BOOL CodeToUtf8 (const std::string &SSRC, std::string &sdst) { int ineed = MultiByteToWideChar (CP_ACP, 0, SSRC . C_STR (),-1, NULL, 0); if (0 = = ineed) { WriteToLog ("Util::codetoutf8:multibytetowidechar = = 0, errorcode=%d, ssrc=%s", GetLastError ( ), Ssrc.c_str ()); return false; } Std::wstring swstr (ineed, 0); MultiByteToWideChar (CP_ACP, 0, Ssrc.c_str (),-1, &swstr[0], swstr.size ()); Ineed = WideCharToMultiByte (Cp_utf8, 0, Swstr.c_str (),-1, NULL, 0, NULL, NULL); if (0 = = ineed) { WriteToLog ("Util::codetoutf8:widechartomultibyte = = 0, errorcode=%d", GetLastError ()); return false; } Sdst.resize (ineed, 0); WideCharToMultiByte (Cp_utf8, 0, Swstr.c_str (),-1, &sdst[0], sdst.size (), NULL, NULL); return true;}
Decode, Utf-8 to current default encoding
BOOLDecodeFromUtf8 (ConstSTD::string&SSRC, std::string&SDST) { intIneed = MultiByteToWideChar (Cp_utf8,0, Ssrc.c_str (),-1Null0); if(0==ineed) {WriteToLog ("util::D Ecodefromutf8:multibytetowidechar = = 0, errorcode=%d, ssrc=%s", GetLastError (), SSRC.C_STR ()); return false; } std::wstring swstr (Ineed,0); MultiByteToWideChar (Cp_utf8,0, Ssrc.c_str (),-1, &swstr[0], swstr.size ()); Ineed= WideCharToMultiByte (CP_ACP,0, Swstr.c_str (),-1Null0, NULL, NULL); if(0==ineed) {WriteToLog ("util::D ecodefromutf8:widechartomultibyte = = 0, errorcode=%d", GetLastError ()); return false; } sdst.resize (Ineed,0); WideCharToMultiByte (CP_ACP,0, Swstr.c_str (),-1, &sdst[0], sdst.size (), NULL, NULL); return true;}
Windows character encoding