/*
   This subroutine converts a string from Hex to decimal. If there is an
   invalid Hexnumber in the string, then the subroutine will return -1
*/

#include <stdio.h>
#include "xasm.h"

int atoh( char *str )
{
WORD val = 0; /* Resulting Value */
BYTE valid = 0;
	
while ( *str == ' ' && *str != 0 && *str != '\n' )
   str++;

while ( (*str >= '0' && *str <= '9') ||
        (*str >= 'a' && *str <= 'f') ||
        (*str >= 'A' && *str <= 'F') )
   {
   valid = 1;
   if ( *str >= 'a' && *str <= 'z' )
      val = (val * 16) + (*str & 0xff) - 0x57;
   if ( *str >= 'A' && *str <= 'Z' )
      val = (val * 16) + (*str & 0xff) - 0x37;
   if ( *str >= '0' && *str <= '9' )
      val = (val * 16) + (*str & 0xff) - 0x30;
   str++;
   }

if ( valid && *str == 0 )
   return ( (int) val ); /* Good Value */
 else
   {
   xerror( "atoh: Conversion Error" );
   return ( -1 ); /* Bad Value */
   }
}
