-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertStrUtil.cpp
More file actions
40 lines (30 loc) · 912 Bytes
/
Copy pathConvertStrUtil.cpp
File metadata and controls
40 lines (30 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "ConvertStrUtil.h"
#include <codecvt>
#include <cstdlib>
#include <clocale>
#include <vector>
namespace lzstring
{
std::string to_string(std::wstring const &src)
{
std::setlocale(LC_CTYPE, "");
std::string dest;
size_t const mbs_len = wcstombs(NULL, src.c_str(), 0);
std::vector<char> tmp(mbs_len + 1);
wcstombs(&tmp[0], src.c_str(), tmp.size());
dest.assign(tmp.begin(), tmp.end() - 1);
return dest;
}
// 把一个string转化为wstring
std::wstring to_wstring(std::string const &src)
{
// std::setlocale(LC_CTYPE, "");
std::setlocale(LC_CTYPE, "zh_CN");
std::wstring dest;
size_t const wcs_len = mbstowcs(NULL, src.c_str(), 0);
std::vector<wchar_t> tmp(wcs_len + 1);
mbstowcs(&tmp[0], src.c_str(), src.size());
dest.assign(tmp.begin(), tmp.end() - 1);
return dest;
}
} // namespace lzstring