关于ANSI和Unicode、Unicode和UTF-8等的相互转换可通用的代码如下:
qp::StringW Global::AnsiToUnicode(
const
char
* buf)
{
int
len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if
(len == 0)
return
L
""
;
std::vector<</CODE>wchar_t
> unicode(len);
::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return
&unicode[0];
}
qp::StringA Global::UnicodeToAnsi(
const
wchar_t
* buf)
{
int
len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
if
(len == 0)
return
""
;
std::vector<</CODE>char
> utf8(len);
::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
return
&utf8[0];
}
qp::StringW Global::Utf8ToUnicode(
const
char
* buf)
{
int
len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
if
(len == 0)
return
L
""
;
std::vector<</CODE>wchar_t
> unicode(len);
::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
return
&unicode[0];
}
qp::StringA Global::UnicodeToUtf8(
const
wchar_t
* buf)
{
int
len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if
(len == 0)
return
""
;
std::vector<</CODE>char
> utf8(len);
::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
return
&utf8[0];
}