Converting double to LPTSTR << Back



Without using CRT, how do you convert a double to a string (LPTSTR/TCHAR[]), and vice versa? By using VariantChangeType!

double StringToDouble(LPCTSTR lpszNumber)
{
	VARIANT vFrom, vTo;
	vFrom.vt = VT_BSTR;
#ifdef UNICODE
	vFrom.bstrVal = SysAllocString(lpszNumber);
#else
	WCHAR swzWide[64];
	MultiByteToWideChar(CP_ACP, 0, lpszNumber, -1, swzWide, 64);
	vFrom.bstrVal = SysAllocString(swzWide);
#endif
	VariantInit(&vTo);
	VariantChangeType(&vTo, &vFrom, 0, VT_R8);
	SysFreeString(vFrom.bstrVal);
	return vTo.dblVal;
}

void DoubleToString(const double num, LPTSTR lpsz, DWORD dwSize)
{
	VARIANT vFrom, vTo;
	vFrom.vt = VT_R8;
	vFrom.dblVal = num;
	VariantInit(&vTo);
	VariantChangeType(&vTo, &vFrom, 0, VT_BSTR);
#ifdef UNICODE
	lstrcpyn(lpsz, vTo.bstrVal, dwSize);
#else
	WideCharToMultiByte(CP_ACP, 0, vTo.bstrVal, -1, lpsz, dwSize, NULL, NULL);
#endif
	SysFreeString(vTo.bstrVal);
}
Free Web Hosting