Author Topic: Looking for improvments - ToBase10  (Read 1849 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
Looking for improvments - ToBase10
« on: May 25, 2009, 01:43:04 PM »
I searched on line and was unable to find a routine that did quite what I needed.  Initially this started out as a binary to hex converter.  Needless to say it changed a bit.

This is going to be part of a routine to perform a CRC32 check on text strings and therefore will need to be about as fast as possible.  That said, I haven't spent any time optimizing for speed.

All suggestions welcome.

Mike

Code: [Select]
;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  Routine: ToBase10
  Purpose: Convert a number from any base between 2 and 36 to base 10
  Arguments: NumBase - integer where 2<= NumBase <= 36.  The number base from which we are
    converting.
  InputValue
    Symbol - example: '010010
    Number - example:   10010
    string - example: "010010" or "10010"
  Returns: The input value converted to base 10
=============================================================================================
  This routine is the inverse of the base routine found in the acad developement help files
    under "ASCII Code Conversion", hence, (tobase10 16 (base 16 5284)) returns 5284
---------------------------------------------------------------------------------------------
  Example1: (tobase10 2 '11001011 )  returns      203
  Example2:     (tobase10 2  11001011 )  returns      203
  Example3:     (tobase10 2 "11001011")  returns      203
 
  Example4:     (tobase10 6 '15243 )     returns     2475
  Example5:     (tobase10 6  15243 )     returns     2475
  Example6:     (tobase10 6 "15243")     returns     2475
 
  Example7:     (tobase10 16 '34F1A )    returns   216858
  Example9:     (tobase10 16 "34F1A")    returns   216858
    Notice that '34F1A cannot be represented as a number, hence, only two examples
 
  Example10: (tobase10 36 '11001011 ) returns 60525828
  Example11:    (tobase10 36  11001011 ) returns 60525828
  Example12:    (tobase10 36 "11001011") returns 60525828
 
    As a check, use: http://pages.swcp.com/~spsvs/resume/BaseConversion/BaseConversion.html
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;
(defun ToBase10 (NumBase InputValue / rtval)
  (cond
    ((= 'str (type inputvalue))
     (setq inputvalue (strcase inputvalue))
     )
    ((= 'SYM (type inputvalue))
     (setq InputValue (vl-symbol-name InputValue))
     )
    )
  (cond
    ;;Base is not an integer
    ((not (= 'int (type NumBase)))
     (strcat
       "ERROR|ToBase10 base argument must be an integer - "
       (vl-prin1-to-string (type NumBase))
       " provided (" (vl-prin1-to-string NumBase) ")"
       )
     )
    ;;Base argument is out of range
    ((or
       (> 2 NumBase)
       (< 36 NumBase)
       )
     (strcat
       "ERROR|ToBase10 Base argument must be greater than 1 and less than 36 - " (itoa NumBase) " provided."
       )
     )
    ;;InputValue is of wrong type for specified base
    ((and
       (> NumBase 10)
       (not (= 'STR (type InputValue)))
       )
       "ERROR|Numbers in a base greater than 10 must be expressed as a string - ToBase routine."
    )
    ;;InputValue has invalid characters
    ((not (vl-every
    (function
      (lambda (asciicode)
(cond
  ;;Character below "0" used
  ((< asciicode 48) nil)
  ;;Character greater than valid used for base less than or equal to 10
  ((and
     (<= NumBase 10)
     (> asciicode (+ 47 NumBase))
     )
   nil
   )
  ;;For base greater than 10, invalid character used
  ((and (> NumBase 10)
(not (wcmatch (chr asciicode) (strcat "[0-9],[A-" (chr (+ NumBase 54)) "]")))
)
   nil
   )
  (T T)
  )
)
      )   
    (vl-string->list (if (= 'INT (type InputValue))
       (itoa InputValue)
       InputValue
       )
      )
  )
     )
     (strcat
       "ERROR|Invalid InputValue argument provided to ToBase10 routine : " (vl-prin1-to-string InputValue)
     )
    )
    (T
     ;; "11001011" binary  203 decimal
     ;;  |||||||L-1*2^0 =   1
     ;;  ||||||L--1*2^1 =   2
     ;;  |||||L---0*2^2 =   0
     ;;  ||||L----1*2^3 =  16
     ;;  |||L-----0*2^4 =   0
     ;;  ||L------0*2^5 =   0
     ;;  |L-------1*2^6 =  64
     ;;  L--------1*2^7 = 128
     ;;         The sum = 203
     ;;  or
     ;; "15243" base 6 to 2475 decimal
     ;;  ||||L----3*6^0 =    3
     ;;  |||L-----4*6^1 =   24
     ;;  ||L------2*6^2 =   72
     ;;  |L-------5*6^3 = 1080
     ;;  L--------1*6^4 = 1296
     ;;         The sum = 2475
     ;;  or
     ;; "34F1A" base 16 to     decimal
     ;;  ||||L--10*16^0 =      10
     ;;  |||L--- 1*16^1 =      16
     ;;  ||L----15*16^2 =    3840
     ;;  |L----- 4*16^3 =   16384
     ;;  L------ 3*16^4 =  196608
     ;;         The sum =  216858
     ;;
     ;;  concept compliments of: http://www.shunsoft.net/ipcalc/help/chap09.html
     (setq
       indx   0
       ;;Get the list of multipliers
       temp (mapcar
      (function
(lambda (x)
  (if (< 57 x)
    (- x 55)
    (- x 48)
  )
)
      )
      (reverse (vl-string->list
(if (= 'INT (type InputValue))
   (itoa InputValue)
   InputValue
)
       )
      )
    )
       rtval   0
     )
     (repeat (length temp)
       (setq
digit (nth indx temp)
rtval (+ rtval (* Digit (expt NumBase indx)))
indx  (1+ indx)
       )
     )
     rtval
    )
  )
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Looking for improvments - ToBase10
« Reply #1 on: May 02, 2010, 05:27:26 PM »
See here  :-)