设为首页 加入收藏

TOP

ansi utf8与unicode互转函数
2014-11-24 00:11:56 来源: 作者: 【 】 浏览:18
Tags:ansi utf8 unicode 函数

乱码伤不起啊。。。。

下面是转换代码。。。

inline wstring utf8ToUnicode(string str)

{

//UTF8 to Unicode

size_t wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), str.length(), NULL, 0);

//分配空间要给''留个空间,MultiByteToWideChar不会给''空间

wchar_t* wszString = new wchar_t[wcsLen + 1];

memset(wszString, 0, sizeof(wchar_t)*(wcsLen + 1));

//转换

::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), str.length(), wszString, wcsLen);

wstring wstr= wszString;

delete wszString;

return wstr;

}

inline string unicode2utf8(wstring wstr)

{

int len; www.2cto.com

len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);

char *szUtf8=new char[len*2 + 2];

memset(szUtf8, 0, len * 2 + 2);

WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wstr.c_str(), -1, szUtf8, len, NULL,NULL);

string str= szUtf8;

delete szUtf8;

return str;

}

摘自 ccSec | cc@insight-labs.org

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇从C语言实战谈编程:vi(编辑源程.. 下一篇CString出错 _free_dbg(p, _NORMA..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: