Author Topic: -= {Challenge} =- convert an hexadecimal into a decimal  (Read 15920 times)

0 Members and 1 Guest are viewing this topic.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #30 on: October 25, 2011, 12:26:40 PM »
Quote from: ElpanovEvgeniy
You did a very concise program!
My version is somewhat more complicated ...
Very nice algorithm VovKa  8-)

thanx guys, but, as i wrote earlier, the algo is not mine. i've just translated the code to lisp. that was a couple of years ago
i'm googling for the original article for half an hour or so, but can't find it :(

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #31 on: October 25, 2011, 12:39:00 PM »
This is an interesting modification to see the progression:  :-)

Code: [Select]
(defun vk_BaseToBase ( h ib ob / lst n )
    (repeat (1+ (fix (* (strlen h) (/ (log ib) (log ob))))) (setq lst (cons 0 lst)))
    (foreach c (vl-string->list (strcase h))
        (setq n (- c (if (< c 58) 48 55)))
        (setq lst
            (mapcar
                (function
                    (lambda ( d )
                        (setq d (+ (* d ib) n) n (/ d ob))
                        (rem d ob)
                    )
                )
                lst
            )
        )
        (print lst)
    )
    (apply 'strcat
        (reverse
            (mapcar
                (function
                    (lambda (c) (chr (+ c (if (< c 10) 48 55))))
                )
                lst
            )
        )
    )
)

Code: [Select]
_$ (vk_BaseToBase "7DB" 16 10)

(7 0 0 0)
(5 2 1 0)
(1 1 0 2) "2011"

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #32 on: October 25, 2011, 04:17:44 PM »
Very nice codes guys !

My regret today, is to have been too lazy to go further searching a nice LISP solution for this challenge.
It would have been my little tribute to John McCarthy...
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #33 on: October 26, 2011, 03:34:08 PM »
my new version:

Code: [Select]
(defun eea-hex2dec (s / L f1 f2 f3)
  ;; By ElpanovEvgeniy
  ;; 26.10.2011
  (defun f1 (i)
    (if (> i 9)
      (append (f1 (rem i 10)) (f1 (/ i 10)))
      (list i)
    )
  )
  (defun f2 (l)
    (if l
      (cons (car l) (f2 (mapcar (function (lambda (a) (cons 0 a))) (cdr l))))
    )
  )
  (defun f3 (l / f)
    (if l
      (cons (apply (function +) (mapcar (function car) l)) (f3 (vl-remove nil (mapcar (function cdr) l))))
    )
  )
  (setq s (mapcar (function (lambda (a) (- a 48)))
                  (vl-string->list (vl-string-translate "abcdef" ":;<=>?" s))
          )
        l '(0)
  )
  (foreach a s
    (setq l (cons (+ (* 16 (car l)) a) (mapcar (function (lambda (a) (* a 16))) (cdr l)))
          l (f3 (f2 (mapcar (function f1) l)))
    )
  )
  (while (vl-some (function (lambda (a) (> a 9))) l) (setq l (f3 (f2 (mapcar (function f1) l)))))
  (apply (function strcat) (mapcar (function itoa) (reverse l)))
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #34 on: October 26, 2011, 09:16:32 PM »
my new version:

Code: [Select]
(defun eea-hex2dec (s / f)
  ;; By ElpanovEvgeniy
  ;; 27.10.2011
  (defun f (l n s)
    (cond ((and (> n 9) (not l)) (f '(0) n nil))
          ((and n l)
           (f (cons (rem (+ (* (car l) 16) n) 10) (f (cdr l) (/ (+ (* (car l) 16) n) 10) nil)) (car s) (cdr s))
          )
          (l)
          ((list n))
    )
  )
  (setq s (mapcar (function (lambda (a) (- a 48)))
                  (vl-string->list (vl-string-translate "ABCDEF" ":;<=>?" (strcase s)))
          )
  )
  (apply (Function strcat) (mapcar (function itoa) (reverse (f '(0) (car s) (cdr s)))))
)


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #35 on: October 27, 2011, 06:57:18 AM »
new version:

Code: [Select]
(defun hex2dec (s / f)
  ;; By ElpanovEvgeniy
  ;; 27.10.2011
  (defun f (l s / a)
    (cond ((and s l) (f (cons (rem (setq a (+ (* (car l) 16) (car s))) 10) (f (cdr l) (list (/ a 10)))) (cdr s)))
          ((> (car s) 9) (f nil (cons (rem (car s) 10) (f nil (list (/ (car s) 10))))))
          (l)
          ((if (not (equal s '(0))) s nil))
    )
  )
  (setq s (mapcar (function (lambda (a) (- a 48))) (vl-string->list (vl-string-translate "ABCDEF" ":;<=>?" (strcase s)))))
  (apply (Function strcat) (mapcar (function itoa) (reverse (f '(0) s))))
)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #36 on: October 27, 2011, 07:38:58 AM »
Someone's been busy!  :lol:

I shall have to take some time to study these  :-)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #37 on: October 28, 2011, 07:57:45 AM »
Evgeniy,

This:

Code: [Select]
((if (not (equal s '(0))) s nil))
could be reduced to:

Code: [Select]
((not (equal s '(0))) s)
 :-)


Fantastic code.
« Last Edit: October 28, 2011, 09:34:04 AM by Lee Mac »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #38 on: October 28, 2011, 10:40:59 AM »
Evgeniy,

This:

Code: [Select]
((if (not (equal s '(0))) s nil))
could be reduced to:

Code: [Select]
((not (equal s '(0))) s)
 :-)


Fantastic code.

Mistakes really fantastic  :-D  :-D  :-D

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #39 on: January 16, 2016, 09:52:41 AM »
Not quite as succinct as some of the other functions, but here is another entry:
Code - Auto/Visual Lisp: [Select]
  1. ;; Hex to Decimal String  -  Lee Mac
  2. ;; Returns the decimal representation of a supplied hexadecimal string
  3.  
  4. (defun LM:hex->decstr ( hex / foo bar )
  5.     (defun foo ( lst rtn )
  6.         (if lst
  7.             (foo (cdr lst) (bar (- (car lst) (if (< 57 (car lst)) 55 48)) rtn))
  8.             (apply 'strcat (mapcar 'itoa (reverse rtn)))
  9.         )
  10.     )
  11.     (defun bar ( int lst )
  12.         (if lst
  13.             (if (or (< 0 (setq int (+ (* 16 (car lst)) int))) (cdr lst))
  14.                 (cons (rem int 10) (bar (/ int 10) (cdr lst)))
  15.             )
  16.             (bar int '(0))
  17.         )
  18.     )
  19.     (foo (vl-string->list (strcase hex)) nil)
  20. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:hex->decstr "1234567890abcdef")
  2. "1311768467294899695"
« Last Edit: January 16, 2016, 10:09:23 AM by Lee Mac »

squirreldip

  • Newt
  • Posts: 114
Re: -= {Challenge} =- convert an hexadecimal into a decimal
« Reply #40 on: January 18, 2016, 04:13:48 PM »
Here's a stab at it...

Code - Auto/Visual Lisp: [Select]
  1. (defun HEX2DEC (S / D N SL STR+)
  2.  (defun STR+ (A B / C D S)
  3.   (setq A (vl-list->string (reverse (vl-string->list A))))
  4.   (setq B (vl-list->string (reverse (vl-string->list B))))
  5.   (setq D "")
  6.   (setq C 0)
  7.   (while (or (/= A "") (/= B ""))
  8.    (setq S (+ C (atoi (substr A 1 1)) (atoi (substr B 1 1))))
  9.    (if (>= S 10)
  10.     (setq C 1 S (- S 10))
  11.     (setq C 0)
  12.    )
  13.    (setq D (strcat (itoa S) D))
  14.    (setq A (substr A 2) B (substr B 2))
  15.   )
  16.   (if (= C 1) (setq D (strcat "1" D)))
  17.   D
  18.  )
  19.  (setq D "0")
  20.  (setq SL (vl-string->list (strcase S)))
  21.  (foreach N SL
  22.   (repeat 4
  23.    (setq D (STR+ D D))
  24.   )
  25.   (setq N (itoa (- N (if (< N 65) 48 55))))
  26.   (setq D (STR+ D N))
  27.  )
  28.  D
  29. )

Code - Auto/Visual Lisp: [Select]
  1. (hex2dec "7ffffcf5c20") ;=>> "8796089834528"
  2. (hex2dec "1234567890abcdef") ;=>> "1311768467294899695"
  3. (hex2dec "fedcba0987654321") ;=>> "18364757930599072545"
  4. (hex2dec "111111111111") ;=>> "18764998447377"
  5. (hex2dec "ffffffffffff") ;=>> "281474976710655"
  6.  
  7. and a big one:
  8. (hex2dec "fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") ;=>> "115277453876119746492122989728511096337595255118815003943225442645743491171105"
« Last Edit: January 18, 2016, 04:51:47 PM by squirreldip »