(hex2dec "7ffffcf5c20") ;=>> "8796089834528"
(hex2dec "1234567890abcdef") ;=>> "1311768467294899695"
(hex2dec "fedcba0987654321") ;=>> "18364757930599072545"
(hex2dec "111111111111") ;=>> "18764998447377"
(hex2dec "ffffffffffff") ;=>> "281474976710655"
(defun hex2dec (hex / r c)
(setq r 0.)
(foreach c (vl-string->list (strcase hex))
(setq r (+
(* 16. r)
(-
c
(if (<= c 57)
48.
55.
)
)
)
)
)
(rtos r 2 0)
)
_$ (hex2dec "7ffffcf5c20")
"8796089834528"
_$ (hex2dec "111111111111")
"18764998447377"
_$ (hex2dec "ffffffffffff")
"281474976710655"
_$ (hex2dec "1234567890abcdef")
"1E+18"
_$ (hex2dec "fedcba0987654321")
"2E+19"
(defun hex2dec ( n )
(defun _ascii2int ( x )
(- x (if (< x 58) 48.0 55.0))
)
(defun _hex2dec ( l )
(if l (+ (* 16.0 (_hex2dec (cdr l))) (_ascii2int (car l))) 0)
)
(defun _dec2str ( n x / d )
(if (< 0 n)
(progn
(setq d (rem (/ n x) 1))
(strcat (_dec2str (- n (* x d)) (* x 10.0)) (rtos (* 10 d) 2 0))
)
""
)
)
(_dec2str (_hex2dec (reverse (vl-string->list (strcase n)))) 10.0)
)
(hex2dec "7ffffcf5c20") ;=>> "8796089834528"
(hex2dec "1234567890abcdef") ;=>> "1311768467294899800"
(hex2dec "fedcba0987654321") ;=>> "18364757930599073000"
(hex2dec "111111111111") ;=>> "18764998447377"
(hex2dec "ffffffffffff") ;=>> "281474976710655"
(defun hex2dec (s / e c l)
;;;SINGLE HEX STRING 0-F to Decimal Conversion
(defun hex2dec1 (s / n)
(setq n (ascii (strcase s)))
(- n (if (> n 57) 55 48)))
(setq e 0
s (strcase s))
(while (> (strlen s) 0)
(setq c (substr s (strlen s) 1)
s (substr s 1 (1- (strlen s)))
l (cons (* (hex2dec1 c) (expt 2 e)) l)
e (+ e 4)))
(apply '+ l))
I hit the same stumbling block:
(1+ 2147483647) = -2147483648
(defun bin? (nu / lst nu)
(while (not (= nu 0))
(setq lst (cons (rem nu 2) lst)
nu (/ nu 2)))
lst
)
Command: (bin? 2147483647)
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
Command: (length (bin? 2147483647))
31
The number 2147483647 is the largest value you can have for a signed int. In C++ you would have to use an unsigned int which would allow you to have an int with a value of 4294967295.
but that does not stop to consider list or texts, for the storage of any size integer :-)
let hex2dec (hex : string) =
let rec foo chrs result =
match chrs with
| [] -> result
| h :: t -> foo t (16I * result + h - (if h <= 57I then 48I else 55I))
string (foo (hex.ToUpper() |> Seq.toList |> List.map (fun c -> bigint(int c))) 0I)
> hex2dec "7ffffcf5c20" ;;
val it : string = "8796089834528"
> hex2dec "1234567890abcdef" ;;
val it : string = "1311768467294899695"
> hex2dec "fedcba0987654321" ;;
val it : string = "18364757930599072545"
> hex2dec "111111111111" ;;
val it : string = "18764998447377"
> hex2dec "ffffffffffff" ;;
val it : string = "281474976710655"
This is much easier with F# wich has a BigInterger type.
(setq s "ffffffffffffffffffffffffffffffffffffffffffffffffff")
(strlen s) ;=>> 50
(hex2dec "ffffffffffffffffffffffffffffffffffffffffffffffffff")
;=>> "1606938044258990275541962092341162602522202993782792835301375"
(vla-get-Utility (vla-get-ActiveDocument (vlax-get-acad-object)))
(vlax-invoke
(vla-get-Utility (vla-get-ActiveDocument (vlax-get-acad-object)))
'GetObjectIDString
(vlax-ename->vla-object (car (entsel)))
)
; error: ActiveX Server returned the error: unknown name: "GETOBJECTIDSTRING"
_$
Hi Lee Mac!
In your code has a bug.
See if you have
(hex2dec "1234567890abcdef"); =>> "1311768467294899800"
must
(hex2dec "1234567890abcdef"); =>> "1311768467294899695"
_$ (dec->string (base->dec "7ffffcf5c20" 16))
"879689834528"
_$ (dec->string (base->dec "1311768467294899695" 16))
"5627943617999796174848"
_$ (dec->string (base->dec "1234567890abcdef" 16))
"1311768467294899712"
_$ (dec->string (base->dec "fedcba0987654321" 16))
"18364757930599071744"
_$ (dec->string (base->dec "111111111111" 16))
"18764998447377"
_$ (dec->string (base->dec "ffffffffffff" 16))
"281474976710655"
Exactly! I get a similar, yet slightly different error using my general purpose routines:Code: [Select]_$ (dec->string (base->dec "7ffffcf5c20" 16))
"879689834528"
_$ (dec->string (base->dec "1311768467294899695" 16))
"5627943617999796174848"
_$ (dec->string (base->dec "1234567890abcdef" 16))
"1311768467294899712"
_$ (dec->string (base->dec "fedcba0987654321" 16))
"18364757930599071744"
_$ (dec->string (base->dec "111111111111" 16))
"18764998447377"
_$ (dec->string (base->dec "ffffffffffff" 16))
"281474976710655"
(dec->string (base->dec "1234567890abcdef" 16)); =>> "1311768467294899712"
must(hex2dec "1234567890abcdef"); =>> "1311768467294899695"
Yet another point where AutoLisp has fallen behind CL. That 2147483647 is the maximum 32bit signed integer value available, now with 64bit programs it's just not good enough for stuff like ID's.
(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 h)
(setq n (- c
(if (< c 58)
48
55
)
)
lst (mapcar (function (lambda (d)
(setq d (+ (* d ib) n)
n (/ d ob)
)
(rem d ob)
)
)
lst
)
)
)
(apply 'strcat
(reverse (mapcar (function (lambda (c)
(chr (+ c
(if (< c 10)
48
55
)
)
)
)
)
lst
)
)
)
)
;;;(vk_BaseToBase (vk_BaseToBase "FFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 16 10) 10 16)
translated from the stolen vb codeCode: [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 h)
(setq n (- c
(if (< c 58)
48
55
)
)
lst (mapcar (function (lambda (d)
(setq d (+ (* d ib) n)
n (/ d ob)
)
(rem d ob)
)
)
lst
)
)
)
(apply 'strcat
(reverse (mapcar (function (lambda (c)
(chr (+ c
(if (< c 10)
48
55
)
)
)
)
)
lst
)
)
)
)
;;;(vk_BaseToBase (vk_BaseToBase "FFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 16 10) 10 16)
(setq s "FFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
(strlen s) ;== 29
(strlen (vk_BaseToBase (vk_BaseToBase s 16 10) 10 16)) ;== 30
yep, leading zeros to be removed via vl-string-left-trim
(defun hex2dec ( str / l1 l2 mat pas vec x x1 )
(setq vec (mapcar '(lambda ( x ) (- x (if (< x 58) 48 55))) (vl-string->list (strcase str)))
pas (_pascalmatrix (length vec) '((1.0)))
mat pas
)
(repeat 5 (setq mat (mxm mat pas)))
(setq l1 (reverse (vxm vec mat))
x1 (car l1)
)
(while (< 0 x1)
(setq l2 (cons (chr (+ (fix (rem x1 10)) 48)) l2)
l1 (cdr l1)
x1 (+ (/ (fix x1) 10) (cond ((car l1)) (0)))
)
)
(apply 'strcat l2)
)
(defun _pascalmatrix ( n l )
(if (< n 2)
l
(_pascalmatrix (1- n)
(cons
(mapcar '+ (cons 0.0 (car l)) (append (car l) '(0.0)))
(mapcar (function (lambda ( x ) (cons 0.0 x))) l)
)
)
)
)
;; Matrix Transpose - Doug Wilson
;; Args: m - nxn matrix
(defun trp ( m )
(apply 'mapcar (cons 'list m))
)
;; Matrix x Matrix - Vladimir Nesterovsky
;; Args: m,n - nxn matrices
(defun mxm ( m n )
( (lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
)
;; Matrix x Vector - Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n
(defun mxv ( m v )
(mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)
(defun vxm ( v m )
(mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) (trp m))
)
(defun eea-hex2dec (s / L f1 f2 f3 f4 f5)
(defun f1 (s)
(mapcar (function (lambda (a)
(- a
(if (<= a 57)
48
55
)
)
)
)
(vl-string->list (strcase s))
)
)
(defun f3 (i)
(defun f2 (i)
(if (> i 9)
(append (f2 (/ i 10)) (f2 (rem i 10)))
(list i)
)
)
(reverse (f2 i))
)
(defun f4 (l)
(if l
(cons (car l) (f4 (mapcar (function (lambda (a) (cons 0 a))) (cdr l))))
)
)
(defun f5 (l / f)
(if l
(cons (apply (function +) (mapcar (function car) l)) (f5 (vl-remove nil (mapcar (function cdr) l))))
)
)
(setq s (f1 s)
l '(0)
)
(while s
(setq l (mapcar (function (lambda (a) (* a 16))) l)
l (cons (+ (car l) (car s)) (cdr l))
s (cdr s)
)
(setq l (f5 (f4 (mapcar (function f3) l))))
)
(while (vl-some (function (lambda (a) (> a 9))) l) (setq l (f5 (f4 (mapcar (function f3) l)))))
(apply (function strcat) (mapcar (function itoa) (reverse l)))
)
You did a very concise program!
My version is somewhat more complicated ...
Very nice algorithm VovKa 8-)
(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
)
)
)
)
_$ (vk_BaseToBase "7DB" 16 10)
(7 0 0 0)
(5 2 1 0)
(1 1 0 2) "2011"
(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)))
)
(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)))))
)
(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))))
)
((if (not (equal s '(0))) s nil))
((not (equal s '(0))) s)
Evgeniy,
This:Code: [Select]((if (not (equal s '(0))) s nil))
could be reduced to:Code: [Select]((not (equal s '(0))) s)
:-)
Fantastic code.