C++ 类型转换(全)

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值