您的位置:首页 > 其它

关于ANSI和Unicode、Unicode和UTF-8等的相互转换

2013-10-16 10:03 274 查看
关于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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: