string 转 int
int number = stoi(string类型字符串);
int 转 string
string str = to_string(你的int型变量);
string 转 char[]
char arr[256];
strcpy(arr,你的字符串.c_str());
string 转 char*(防乱码)
char* stringToCharP(std::string str){
// 初始化 result
char* result = new char[str.length()+1];
// 赋值 result
strcpy_s(result,str.length()+1,str.c_str());
return result;
}
char[] 转 DWORD
// keyValue 为 char[]
DWORD dw_keyValue = keyValue[0] | keyValue[1] << 8 | keyValue[2] << 16 | keyValue[3] << 24;
DWORD - string 互转
// string 转 dword
DWORD stringToDword(string val){
DWORD cur_dword;
sscanf(val.c_str(),"%ul",&cur_dword);
return cur_dword;
}
// dword 转 string
string dwordToString(DWORD val){
string cur_str = to_string(long long (val));
return cur_str;
}
10进制 转 16进制
char buf[24];
_itoa(十进制整形变量, buf, 16);
wchar_t* 转 string
string wchart_to_string(wchar_t *wchar)
{
string szDst;
wchar_t * wText = wchar;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);
char *psText;
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
szDst = psText;
delete []psText;
return szDst;
}
string 转 wstring:
// 不支持中文
wstring string_to_wstring(string str)
{
wstring szDst;
std::string temp = str;
int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL,0);
wchar_t * wszUtf8 = new wchar_t[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
szDst = wszUtf8;
std::wstring r = wszUtf8;
delete[] wszUtf8;
return szDst;
}
// 支持中文
wstring string_to_wstring(string str){
wstring result;
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
if( len < 0 )return result;
wchar_t* buffer = new wchar_t[len + 1];
if( buffer == NULL )return result;
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0';
result.append(buffer);
delete[] buffer;
return result;
}
wstring 转 string
// 不支持中文
string wstring_to_string(const wstring &wstr)
{
string str;
int nLen = (int)wstr.length();
str.resize(nLen, ' ');
int nResult = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), nLen, (LPSTR)str.c_str(), nLen, NULL, NULL);
if (nResult == 0){return "";}
return str;
}
// 支持中文
string wstring_to_string(wstring wstr){
string result;
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
if( len <= 0 )return result;
char* buffer = new char[len + 1];
if(buffer == NULL )return result;
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
result.append(buffer);
delete[] buffer;
return result;
}