One, multi-byte encoding to UTF8 encoding
BOOL MBToUTF8 (vector<char>& pu8, const char* PMB, Int32 mlen)
{
1. Convert an MBCS string to Widechar
Int32 nlen = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, NULL, 0);
wchar* LPSZW = NULL;
Try
{
LPSZW = new Wchar[nlen];
}
catch (Bad_alloc &memexp)
{
return false;
}
Int32 nrtn = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, LPSZW, Nlen);
if (nrtn! = Nlen)
{
Delete[] LPSZW;
return false;
}
2.convert a widechar string to UTF8
Int32 utf8len = WideCharToMultiByte (Cp_utf8, 0, LPSZW, nlen, NULL, 0, NULL, NULL);
if (utf8len <= 0)
{
return false;
}
Pu8.resize (Utf8len);
Nrtn = WideCharToMultiByte (Cp_utf8, 0, LPSZW, Nlen, &*pu8.begin (), utf8len, NULL, NULL);
Delete[] LPSZW;
if (nrtn! = Utf8len)
{
Pu8.clear ();
return false;
}
return true;
}
Second, UTF8 encoding to multi-byte encoding
BOOL Utf8tomb (vector<char>& PMB, const char* Pu8, Int32 utf8len)
{
1.convert a UTF8 string to Widechar
Int32 nlen = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, NULL, 0);
wchar* LPSZW = NULL;
Try
{
LPSZW = new Wchar[nlen];
}
catch (Bad_alloc &memexp)
{
return false;
}
Int32 nrtn = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, LPSZW, Nlen);
if (nrtn! = Nlen)
{
Delete[] LPSZW;
return false;
}
2. Convert an widechar string to multibyte
Int32 mblen = WideCharToMultiByte (CP_ACP, 0, LPSZW, nlen, NULL, 0, NULL, NULL);
if (Mblen <=0)
{
return false;
}
Pmb.resize (Mblen);
Nrtn = WideCharToMultiByte (CP_ACP, 0, LPSZW, Nlen, &*pmb.begin (), mblen, NULL, NULL);
Delete[] LPSZW;
if (nrtn! = Mblen)
{
Pmb.clear ();
return false;
}
return true;
}
three, multibyte encoding to Unicode encoding
BOOL Mbtounicode (vector<wchar_t>& pun, const char* PMB, Int32 mlen)
{
//convert an MBCS string to Widechar
Int32 Ulen = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, NULL, 0);
if (ulen<=0)
{
return false;
}
Pun.resize (Ulen);
Int32 nrtn = MultiByteToWideChar (CP_ACP, 0, PMB, Mlen, &*pun.begin (), Ulen);
if (nrtn! = Ulen)
{
Pun.clear ();
return false;
}
return true;
}
Four, Unicode encoding to multi-byte encoding
BOOL Unicodetomb (vector<char>& PMB, const wchar_t* Pun, Int32 Ulen)
{
Convert an widechar string to multibyte
Int32 mblen = WideCharToMultiByte (CP_ACP, 0, pun, ulen, NULL, 0, NULL, NULL);
if (Mblen <=0)
{
return false;
}
Pmb.resize (Mblen);
int nrtn = WideCharToMultiByte (CP_ACP, 0, Pun, Ulen, &*pmb.begin (), mblen, NULL, NULL);
if (nrtn! = Mblen)
{
Pmb.clear ();
return false;
}
return true;
}
v. UTF8 encoding to Unicode
BOOL Utf8tounicode (vector<wchar_t>& pun, const char* Pu8, Int32 utf8len)
{
Convert an UTF8 string to Widechar
Int32 nlen = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, NULL, 0);
if (Nlen <=0)
{
return false;
}
Pun.resize (Nlen);
Int32 nrtn = MultiByteToWideChar (Cp_utf8, 0, Pu8, Utf8len, &*pun.begin (), Nlen);
if (nrtn! = Nlen)
{
Pun.clear ();
return false;
}
return true;
}
VI. Unicode encoding converted to UTF8
BOOL UnicodeToUTF8 (vector<char>& pu8, const wchar_t* Pun, Int32 Ulen)
{
Convert an widechar string to UTF8
Int32 utf8len = WideCharToMultiByte (Cp_utf8, 0, pun, ulen, NULL, 0, NULL, NULL);
if (utf8len<=0)
{
return false;
}
Pu8.resize (Utf8len);
Int32 nrtn = WideCharToMultiByte (Cp_utf8, 0, Pun, Ulen, &*pu8.begin (), utf8len, NULL, NULL);
if (nrtn! = Utf8len)
{
Pu8.clear ();
return false;
}
return true;
}
Http://www.cppblog.com/deane/articles/120243.html