home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 223_01 / xtoi.c < prev   
Encoding:
C/C++ Source or Header  |  1979-12-31  |  640 b   |  23 lines

  1. #include stdio.h
  2. /*
  3. ** xtoi -- convert hex string to integer nbr
  4. **         returns field size, else ERR on error
  5. */
  6.   static int xd, t;
  7.  
  8. xtoi(hexstr, nbr)  char *hexstr;  int *nbr;  {
  9.   xd = 0;
  10.   *nbr=0;
  11.   while(1)
  12.     {
  13.     if((*hexstr>='0')&(*hexstr<='9')) t=48;
  14.     else if((*hexstr>='A')&(*hexstr<='F')) t=55;
  15.     else if((*hexstr>='a')&(*hexstr<='f')) t=87;
  16.     else break;
  17.     if(xd<4) ++xd; else return ERR;
  18.     *nbr=*nbr<<4;
  19.     *nbr=*nbr+(*hexstr++)-t;
  20.     }
  21.   return xd;
  22.   }
  23.