Re: Converting CString to a Number
On Oct 5, 9:47 pm, "JCO" <some...@somewhere.com> wrote:
VS 2008, MFC, C++, Unicode compatibility
I'm having a constant issue of converting numbers to text and text to
numbers.
In this case, I have a 16 digit number in a CString that needs to be
converted to a number (no decimal).
Example
CString strText( _T("1234567890123456") );
these all give me the same number
long lNum = wcstol( strText, NULL , 10 );
unsigned long ulNum = _wtol_l( strText, NULL );
long lNum1 = _wtol( strText );
I had a similar problem earlier converting from Text to a float, but it w=
as
resolved this way (and it works fine);
fTotal += (float) wcstod( strTmp, NULL ); /=
/add to total
Using variants is often a good Windows-specific approach to such
problems. Perhaps something like this does it for you:
COleVariant ConvertToDesiredType(LPCTSTR pszNumberInUnknownFormat,
const VARENUM vt[])
{
COleVariant src(pszNumberInUnknownFormat);
COleVariant dest;
for (size_t i=0; vt[i] != VT_EMPTY; i++)
{
if (SUCCEEDED(VariantChangeType(&dest, &src, 0, VARTYPE(vt[i]))))
return dest;
}
return dest;
}
This functions receives a text and tries to convert it into a variant
of an "acceptable" variant type, as defined by vt. I used VT_EMPTY to
mark the end of vt; but varargs or a vector is just as doable. If
result isn't VT_EMPTY, you managed to convert your text into one of
types you specified. If you e.g. go for simple { VT_I4 , VT_EMPTY },
you force conversion into VT_I4 (or fail).
E.g.
const VARENUM types[VT_I4, VT_I2, VT_I8, VT_R8, VT_EMPTY/
*important*/];
// Tries to convert text to I4, I2, I8, R8, in given order.
COleVariant result = ConvertToDesiredType(_T("1234567890123456"),
types);
if (V_VT(result) == VT_I8)
{
LONGLONG val = V_I8(&result);
WorkWith(val);
}
Added bonus is that variant conversions obey locale settings, which
AFAIK standard library does not, not without specifying the locale
yourself.
Downside is that the above should be heavier on CPU than tcstol (e.g.
because of the conversion from a string to a variant). Normally, that
should not be relevant, and but without measurement in your own
context, you can't know whether it is.
Goran.