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;
}
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献1条内容
所有评论(0)