.std::string转utf8字符串
来源:互联网 发布:华东理工大学知乎事件 编辑:程序博客网 时间:2024/05/07 19:58
- utf8转std::string
转换过程:先将utf8转双字节Unicode编码,再通过WideCharToMultiByte将宽字符转换为多字节。
//———————————————————————–
std::string UTF8_To_string(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete []pBuf;
delete []pwBuf;
pBuf = NULL;
pwBuf = NULL;
return retStr;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// [8/19/2009 Leezhm]
// translate ascii characters to utf-8 characters
2.std::string转utf8字符串
转换过程:与1过程相反
//————————————————————————
std::string string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete []pwBuf;
delete []pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}
//////////////////////////////////////////////////////////////////////////
- .std::string转utf8字符串
- 构造UTF8的std::string
- 构造UTF8的std::string
- utf8与std::string字符编码转换
- std:string格式化字符串
- std::string字符串分割
- string与utf8字符串转换
- CString、std::string格式化字符串
- std::string 字符串大小写转换
- std::string是字符串吗?
- c++ utf8与std::string字符编码转换
- c++ utf8与std::string字符编码转换
- ios9 字符串转UTF8 ,UTF8转字符串
- CString 转 std::string
- std::string 转LPCSTR
- std::string 转 byte[]
- std string转wstring
- std::vector<string>转string
- vs2008 专业版使用到期后升级方法
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 D. Hack Portals(区间DP)
- 网络与信息安全研究方向
- 《Linux操作系统编译构建指南》
- 出现错误:ERROR: no such package '@protobuf//src/google/protobuf': Could not find handler for bind rule /
- .std::string转utf8字符串
- HDU 5533 Dancing Stars on Me (暴力模拟+思维)
- bzoj3672 [ NOI2014 ] -- 树上CDQ分治 + 斜率优化DP
- Math类
- 分布式文件系统HDFS
- 网络设备吞吐量
- python--leetcode657. Judge Route Circle
- 报错:“The content of element type "package" must match "(result-types?,interceptors?,default-intercept
- BufferedReader和BufferedWriter读写中文乱码的解决方法