1、char*转wchar_t*
wchar_t* toolC::char2wchar(const char* cchar)
{
wchar_t* m_wchar;
int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0);
m_wchar = new wchar_t[len+1];
MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len);
m_wchar[len] = '\0';
return m_wchar;
}
2、wchar_t*转char*
char* toolC::wchar2char(const wchar_t* wchar)
{
char* m_char;
int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
m_char = new char[len+1];
WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), m_char, len, NULL, NULL);
m_char[len] = '\0';
return m_char;
}
3、TCHAR* 转char*
char* toolC::TCHARToChar(TCHAR* pTchar)
{
char* pChar = nullptr;
int len = wcslen(pTchar) + 1;
pChar= new char[len * 2];
WideCharToMultiByte(CP_ACP, 0, pTchar, len, pChar, 2 * len, NULL, NULL);
return pChar;
}
4、char* 转TCHAR
TCHAR* toolC::TCHARToChar(char* pChar)
{
TCHAR* pTchar = nullptr;
int len = strlen(pChar) + 1;
pTchar= new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, pChar, len, pTchar, len);
return pTchar;
}
5、string 转double
double toolC::string2num(std::string str)
{
double num = 0.0;
std::stringstream ss;
ss << str;
ss >> num;
return num;
}
6、double 转string
string toolC::num2string(double num)
{
stringstream ssdouble;
ssdouble << num;
string str = ssdouble.str();
return str;
}
7、long double 转string
string toolC::do_fraction(long double value, int decplaces)
{
ostringstream out;
int prec = numeric_limits<long double>::digits10;
out.precision(prec);//覆盖默认精度
out << value;
string str = out.str();//从流中取出字符串
size_t n = str.find(".");
if((n != string::npos) /有小数点
&& (str.size() > n + decplaces))//还有至少decplaces位
{
str[n + decplaces] = '\0';//覆盖第一个多余的数
}
str.swap(string(str.c_str());//删除nul之后的多余字符
return str;
}