Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Конвертируем числовую строку в число (MSVC)


Автор: bel_nikita 20.10.2005, 15:57
Понадобилось на днях конвертация строк в значения. Смастерил и подумал, может тоже кому пригодиться
Код

/*
 *    Convert string to value DECLARATION
 */
LONG    ConvertStrToLONG  (LPCTSTR pszValue, LONG   Default = 0,  int Radix = 0);
DWORD   ConvertStrToDWORD (LPCTSTR pszValue, DWORD  Default = 0,  int Radix = 0);
UINT    ConvertStrToUINT  (LPCTSTR pszValue, UINT   Default = 0,  int Radix = 0);
INT     ConvertStrToINT   (LPCTSTR pszValue, INT    Default = 0,  int Radix = 0);
SHORT   ConvertStrToSHORT (LPCTSTR pszValue, SHORT  Default = 0,  int Radix = 0);
WORD    ConvertStrToWORD  (LPCTSTR pszValue, WORD   Default = 0,  int Radix = 0);
BYTE    ConvertStrToBYTE  (LPCTSTR pszValue, BYTE   Default = 0,  int Radix = 0);
DOUBLE  ConvertStrToDOUBLE(LPCTSTR pszValue, DOUBLE Default = 0.0);
BOOL    ConvertStrToBOOL  (LPCTSTR pszValue, BOOL   Default = FALSE);
bool    ConvertStrTobool  (LPCTSTR pszValue, bool   Default = false);

/************************************************************************/
/* FUNCTION    :  ConvertStrToLONG                                      */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a long-integer value               */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/*                Radix - Number base to use                            */
/* RETURN VALUE:  value or Default                                      */
/* COMMENT     :  none                                                  */
/************************************************************************/
LONG ConvertStrToLONG(LPCTSTR pszValue, LONG Default /* =0 */, int Radix /* = 0 */)
{
    LPTSTR pszStop;
    if ( *pszValue != 0 )
  {
        LONG nResult = _tcstol(pszValue, &pszStop, Radix);
        if (*pszStop == 0)
    {
            return (nResult);
        }
    }
    return (Default);  
}

/************************************************************************/
/* FUNCTION    :  ConvertStrToDWORD                                     */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a unsigned long-integer value      */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/*                Radix - Number base to use                            */
/* RETURN VALUE:  value or Default                                      */
/* COMMENT     :  none                                                  */
/************************************************************************/
DWORD ConvertStrToDWORD(LPCTSTR pszValue, DWORD Default /* =0 */, int Radix /* = 0 */)
{
    LPTSTR pszStop;
    if ( *pszValue != 0 )
  {
        LONG nResult = _tcstoul(pszValue, &pszStop, Radix);
        if (*pszStop == 0)
    {
            return (nResult);
        }
    }
    return (Default);  
}

/************************************************************************/
/* FUNCTION    :  ConvertStrToBOOL                                      */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a BOOL value                       */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/* RETURN VALUE:  value or Default                                      */
/* COMMENT     :  none                                                  */
/************************************************************************/
BOOL ConvertStrToBOOL(LPCTSTR pszValue, BOOL fDefault /* = FALSE */)
{
    return (*pszValue != 0 ? _tcschr(_T("1tTyY"), pszValue[0]) != NULL : fDefault);
}

/************************************************************************/
/* FUNCTION    :  ConvertStrToDOUBLE                                    */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a double-precision value           */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/* RETURN VALUE:  the value of the floating-point number or Default     */
/* COMMENT     :  none                                                  */
/************************************************************************/
DOUBLE ConvertStrToDOUBLE(LPCTSTR pszValue, DOUBLE Default /* = 0.0 */)
{
    LPTSTR pszStop;
    if ( *pszValue != 0 )
  {
        DOUBLE nResult = _tcstod(pszValue, &pszStop);
        if (*pszStop == 0)
    {
            return (nResult);
        }
    }
    return (Default);    
}

/************************************************************************/
/* FUNCTION    :  ConvertStrToSHORT                                     */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a short value                      */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/*                Radix - Number base to use                            */
/* RETURN VALUE:  the value of the short number or Default              */
/* COMMENT     :  none                                                  */
/************************************************************************/
SHORT ConvertStrToSHORT(LPCTSTR pszValue, SHORT Default /* =0 */, int Radix /* = 0 */)
{
  return ( static_cast<SHORT>(ConvertStrToLONG(pszValue,Default,Radix)) );
}

/************************************************************************/
/* FUNCTION    :  ConvertStrToWORD                                      */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a unsigned short value             */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/*                Radix - Number base to use                            */
/* RETURN VALUE:  the value of the unsigned short number or Default     */
/* COMMENT     :  none                                                  */
/************************************************************************/
WORD ConvertStrToWORD(LPCTSTR pszValue,WORD Default /* =0 */, int Radix /* = 0 */)
{
  return ( LOWORD(ConvertStrToDWORD(pszValue,Default,Radix)) );
}

/************************************************************************/
/* FUNCTION    :  ConvertStrToBYTE                                      */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a unsigned char value              */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/*                Radix - Number base to use                            */
/* RETURN VALUE:  the value of the unsigned char number or Default      */
/* COMMENT     :  none                                                  */
/************************************************************************/
BYTE  ConvertStrToBYTE(LPCTSTR pszValue,BYTE Default /* =0 */, int Radix /* = 0 */)
{
  return ( LOBYTE(LOWORD(ConvertStrToDWORD(pszValue,Default,Radix))) );  
}

/************************************************************************/
/* FUNCTION    :  ConvertStrTobool                                      */
/* OBJECT      :  none                                                  */
/* PURPOSE     :  Convert strings to a bool-value                       */
/* PARAMETERS  :  pszValue - string                                     */
/*                Default - default value                               */
/* RETURN VALUE:  the value of the bool or Default                      */
/* COMMENT     :  none                                                  */
/************************************************************************/
bool ConvertStrTobool(LPCTSTR pszValue, bool Default /* = false */)
{
    return ( ConvertStrToBOOL(pszValue, Default) != 0 );
}

INT ConvertStrToINT(LPCTSTR pszValue, INT Default /* = 0 */, int Radix /* = 0 */)
{
  return ( static_cast<INT>(ConvertStrToLONG(pszValue,Default,Radix)) );
}

UINT ConvertStrToUINT(LPCTSTR pszValue, UINT Default /* = 0 */, int Radix /* = 0 */)
{
  return ( static_cast<UINT>(ConvertStrToLONG(pszValue,Default,Radix)) );
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)