home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / nonvolatile / XStrToULong.c < prev   
Encoding:
C/C++ Source or Header  |  1993-08-31  |  3.5 KB  |  131 lines

  1. /* xStrToULong.  Converts a hex string to a ULONG.
  2.    Library type of function.  Snarl. 
  3.    Inputs:
  4.          STRPTR - string of form $nnnnnnnn, 0xnnnnnnnn,
  5.                   Xnnnnnnnn, or xnnnnnnnn.
  6.                   There must be 1 to 8 valid hex digits
  7.                   after the introductory character(s).
  8.                   String MUST be null-terminated!
  9.                   String won't be altered.
  10.          ULONG *- pointer to ULONG in which you want the result.
  11.                   ULong will be altered most of the time, whether
  12.                   this function succeeds or fails.
  13.  
  14.    Returns:
  15.          TRUE if conversion succeeded.  ULONG contains converted number.
  16.          FALSE otherwise (number too large, not hex, whatever)
  17.                ULONG contains garbage.
  18. */
  19.  
  20. #include <exec/types.h>
  21.  
  22. /* Prototype */
  23. BOOL xStrToULong(STRPTR str, ULONG *result);
  24.  
  25.  
  26. BOOL xStrToULong(STRPTR str, ULONG *result) {
  27.     STRPTR s = str, tmp;
  28.     int len=0, shifter;
  29.  
  30.     /* Fail if NULL args were given */
  31.     if (!str || !result)
  32.         return(FALSE);
  33.  
  34.     /* look at str[1] */
  35.     s++;
  36.  
  37.     /* position s to point at the first hex digit */    
  38.     switch (*str) {
  39.         case '0':
  40.             if ((*s != 'x') && (*s != 'X'))
  41.                 return(FALSE);
  42.             s++;
  43.             break;
  44.         case '$':
  45.         case 'x':
  46.         case 'X':
  47.             break;
  48.         default:
  49.             return(FALSE);
  50.             break;
  51.     }
  52.  
  53.     /* find out how many hex digits we have */
  54.     tmp = s;
  55.     while (*tmp++)
  56.         len++;
  57.  
  58.     /* fail if we have too many or too few */
  59.     if ((len > 8) || (len == 0))
  60.         return(FALSE);
  61.  
  62.     /* zap out whatever was in *result */
  63.     *result = 0UL;
  64.  
  65.     /* turn string into a number */
  66.     while (*s) {
  67.         shifter = (--len) * 4;
  68.         switch (*s++) {
  69.             case '0':
  70.                 break;
  71.             case '1':
  72.                 *result += (1UL<<shifter);
  73.                 break;
  74.             case '2':
  75.                 *result += (2UL * (1UL<<shifter));
  76.                 break;
  77.             case '3':
  78.                 *result += (3UL * (1UL<<shifter));
  79.                 break;
  80.             case '4':
  81.                 *result += (4UL * (1UL<<shifter));
  82.                 break;
  83.             case '5':
  84.                 *result += (5UL * (1UL<<shifter));
  85.                 break;
  86.             case '6':
  87.                 *result += (6UL * (1UL<<shifter));
  88.                 break;
  89.             case '7':
  90.                 *result += (7UL * (1UL<<shifter));
  91.                 break;
  92.             case '8':
  93.                 *result += (8UL * (1UL<<shifter));
  94.                 break;
  95.             case '9':
  96.                 *result += (9UL * (1UL<<shifter));
  97.                 break;
  98.             case 'a':
  99.             case 'A':
  100.                 *result += (10UL * (1UL<<shifter));
  101.                 break;
  102.             case 'b':
  103.             case 'B':
  104.                 *result += (11UL * (1UL<<shifter));
  105.                 break;
  106.             case 'c':
  107.             case 'C':
  108.                 *result += (12UL * (1UL<<shifter));
  109.                 break;
  110.             case 'd':
  111.             case 'D':
  112.                 *result += (13UL * (1UL<<shifter));
  113.                 break;
  114.             case 'e':
  115.             case 'E':
  116.                 *result += (14UL * (1UL<<shifter));
  117.                 break;
  118.             case 'f':
  119.             case 'F':
  120.                 *result += (15UL * (1UL<<shifter));
  121.                 break;
  122.             default:
  123.                 /* fail if we find a bad digit */
  124.                 return(FALSE);
  125.                 break;
  126.         }
  127.     }
  128.     return(TRUE);
  129. }
  130.  
  131.