string和wstring相互转换方法,标准C++做成,可以在Linux、Windows下运行
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
·
string和wstring相互转换方法,这个是对以前的修改,主要修改了输入为空,导致的小bug。
/*
string 转换为 wstring
*/
std::wstring c2w(const char *pc)
{
std::wstring val = L"";
if(NULL == pc)
{
return val;
}
//size_t size_of_ch = strlen(pc)*sizeof(char);
//size_t size_of_wc = get_wchar_size(pc);
size_t size_of_wc;
size_t destlen = mbstowcs(0,pc,0);
if (destlen ==(size_t)(-1))
{
return val;
}
size_of_wc = destlen+1;
wchar_t * pw = new wchar_t[size_of_wc];
mbstowcs(pw,pc,size_of_wc);
val = pw;
delete pw;
return val;
}
/*
wstring 转换为 string
*/
std::string w2c(const wchar_t * pw)
{
std::string val = "";
if(!pw)
{
return val;
}
size_t size= wcslen(pw)*sizeof(wchar_t);
char *pc = NULL;
if(!(pc = (char*)malloc(size)))
{
return val;
}
size_t destlen = wcstombs(pc,pw,size);
/*转换不为空时,返回值为-1。如果为空,返回值0*/
if (destlen ==(size_t)(0))
{
return val;
}
val = pc;
delete pc;
return val;
}
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐



所有评论(0)