Tuesday, January 11, 2005

strtod

Funny.. strtod doesn't support hexadecimal in FreeBSD. Use this instead:

/*************************************************************************
* NAME:              strtodec
* SUMMARY:      Converts string to decimal number
* RETURNS:      Returns the decimal number.
* AUTHOR:         Sankar N.
************************************************************************/
int strtodec (const CHAR8 *src) {
   /* Integer to hold resulting value */
   int dec;

   /* Check if the string contains a hexadecimal number */
   if ( ('0' == *(src)) && ( ('x' == *(src+1)) || ('X' == *(src+1)) ) ) {
      sscanf (src, "%x", &dec);
   } else {
      /* String contains decimal number */
      sscanf (src, "%d", &dec);
   }
   return (dec);
}

0 Comments:

Post a Comment

<< Home