c++ 数据类型转换
std::string 转 int
string str; int istr = std::atoi(str.c_str());
char* 转 int
char* tok; int itok = atoi(tok);
std::string 或者 const char* 转 char*
string str; char* cstr = const_cast<char*>(str.c_str());
char* 转 CString
char c[80]; CString str; str.Format("%s", c);
char* 转 __int64
const char* htstr; __int64 ht = _atoi64(htstr);
CString 转 __int64
CString str;
__int64 htFile = _atoi64(str);
CString 转 double
CString str; double rate = atof(str);
std::string 转 __int64
std::string str; __int64 htFile = _atoi64(str.c_str());
std::string 转 BSTR
#include<comutil.h> #pragma comment(lib, "comsuppw.lib") std::string str; BSTR bstr = _com_util::ConvertStringToBSTR(str.c_str());
std::string 转 std::wstring (utf8编码转unicode编码)
#include <codecvt> std::string name; std::wstring_convert<std::codecvt_utf8<wchar_t>> wcv; std::wstring wideStr = wcv.from_bytes(name);
或者
string str="yl"; wstring result; int len=MultiByteToWideChar(CP_ACP,0,str.c_str(),str.size(),NULL,0); TCHAR* buffer = new TCHAR[len+1]; MultiByteToWideChar(CP_ACP,0,str.c_str(),str.size(),buffer,len); buffer[len]='\0'; result.append(buffer); delete[] buffer;
C++ wstring和string相互转换的几种方法_Panix2n_n的博客-CSDN博客_c++ wstring转string
std::wstring 转 unsigned char (unicode编码转GBK18030/GBK/GB2312)
unicode 码点、 unicode utf8编码、GB2312/GBK/GB18030编码的互转的例子_brooknew的博客-CSDN博客
std::wstring wideStr; unsigned char bb[256]; WideCharToMultiByte(CP_ACP,0,wideStr.c_str(),-1,(LPSTR)bb,sizeof(bb),NULL,NULL);
std::string 转 sdbyte
std::string 转 std::wstring 转 unsigned char
数值类型 转 std::string
std::string str = std::to_string(数值变量);
gb2312 转 utf-8 std::string
const char* gbstr int len = MultiByteToWideChar(CP_ACP,0,gbstr,-1,NULL,0); wchar_t* wstr = new wchar_t[len+1]; memset(wstr,0,len+1); MultiByteToWideChar(CP_ACP,0,gbstr,-1,wstr,len); len = WideCharToMultiByte(CP_UTF8,0,wstr,-1,NULL,0,NULL,NULL); char* str = new char[len+1]; memset(str,0,len+1); WideCharToMultiByte(CP_UTF8,0,wstr,-1,str,len,NULL,NULL); if(wstr) { delete[] wstr; }