PDA

View Full Version : Hex2dec



kennet.sjoberg
2004-12-21, 05:02 PM
Does anybody have a lisp routine that convert HEX to DEC ?

: ) Happy Computing !

kennet

rkmcswain
2004-12-21, 07:22 PM
Does anybody have a lisp routine that convert HEX to DEC ?

: ) Happy Computing !

kennet
Nope, but it's not hard to find.

http://groups-beta.google.com/group/autodesk.autocad.customization/browse_thread/thread/a0162f8744a2a99e/32aad500cd5bfdc9?q=autolisp+hex+to+decimal&_done=%2Fgroups%3Fq%3Dautolisp+hex+to+decimal%26hl%3Den%26lr%3D%26c2coff%3D1%26safe%3Doff%26sa%3DN%26tab%3Dwg%26&_doneTitle=Back+to+Search&&d#32aad500cd5bfdc9

Tom Beauford
2004-12-21, 07:50 PM
Does anybody have a lisp routine that convert HEX to DEC ?

: ) Happy Computing !

kennet
Search in developer help for "ASCII Code Conversion" and you'll find the following routine:



; BASE converts from a decimal integer to a string in another base.(defun BASE ( bas int / ret yyy zot ) (defun zot ( i1 i2 / xxx ) (if (> (setq xxx (rem i2 i1)) 9) (chr (+ 55 xxx)) (itoa xxx) ) ) (setq ret (zot bas int) yyy (/ int bas)) (while (>= yyy bas) (setq ret (strcat (zot bas yyy) ret)) (setq yyy (/ yyy bas)) ) (strcat (zot bas yyy) ret))(defun C:ASCII ( / chk out ct code dec oct hex ) (initget "Yes") (setq chk (getkword "\nWriting to ASCII.TXT, continue? <Y>: ")) (if (or (= chk "Yes")(= chk nil)) (progn (setq out (open "ascii.txt" "w") chk 1 code 0 ct 0) (princ "\n \n CHAR DEC OCT HEX \n") (princ "\n \n CHAR DEC OCT HEX \n" out) (while chk (setq dec (strcat " " (itoa code)) oct (base 8 code) hex (base 16 code)) (setq dec (substr dec (- (strlen dec) 2) 3)) (if (< (strlen oct) 3)(setq oct (strcat "0" oct))) (princ (strcat "\n " (chr code) " " dec " " oct " " hex ) ) (princ (strcat "\n " (chr code) " " dec " " oct " " hex ) out) (cond ((= code 255)(setq chk nil)) ((= ct 20) (setq xxx (getstring "\n \nPress 'X' to eXit or any key to continue: ")) (if (= (strcase xxx) "X") (setq chk nil) (progn (setq ct 0) (princ "\n \n CHAR DEC OCT HEX \n") ) ) ) ) (setq ct (1+ ct) code (1+ code)) ) (close out) (setq out nil) ) ) (princ))

kennet.sjoberg
2004-12-21, 08:52 PM
Thank You Tom, but I want the reversed one, from HEX => DEC, like (c:hex2dec "FF" ) => 255

I tryid to revers the one You posted, but I run in to a wall...

: ) Happy Computing !

kennet

CADmium
2004-12-22, 09:16 PM
check this :


(defun DT:StrBaseX2INT (ZAHLSTR BASIS / ASC_WERT N INT_ZAHL FEHLER)
(if (and(=(type ZAHLSTR) 'STR)(=(type BASIS)'INT)(> BASIS 0)(< BASIS 37))
(progn
(setq ZAHLSTR (strcase ZAHLSTR))
(while (=(substr ZAHLSTR 1 1) "0") (setq ZAHLSTR(substr ZAHLSTR 2)))
(setq INT_ZAHL 0)
(while (and(>(strlen ZAHLSTR)0)(not FEHLER))
(setq N (ascii(substr ZAHLSTR 1 1)))
(setq ZAHLSTR (substr ZAHLSTR 2))
(if (and(setq N (cond ((< N 48) nil)
((< N 58) (- N 48))
((< N 65) nil)
((< N 91) (- N 55))
)
)
(< N BASIS)
)
(setq INT_ZAHL (+ INT_ZAHL (* (expt BASIS (strlen ZAHLSTR)) N)))
(setq FEHLER 'T)
)
)
(if (not FEHLER) INT_ZAHL)
)
)
)

and than you must run :
(DT:StrBaseX2INT "40A" 16) -> 1034 (= Hex2dec)

the other way :


(defun DT:BaseX2Str(ZAHL BASIS / STRING I)
(setq STRING "")
(if (and(=(type ZAHL)'INT)
(=(type BASIS)'INT)(> BASIS 0)(< BASIS 37)
)
(progn
(while (< 0 ZAHL)
(setq STRING (strcat(if (> (setq I (fix(rem ZAHL BASIS))) 9)
(chr (+ 55 I))
(chr (+ 48 I))
)
STRING
)
)
(setq ZAHL (/ ZAHL BASIS))
)
)
)
STRING
)

there is for instance: (BaseX2Str 1034 16) -> "40A"

kennet.sjoberg
2004-12-23, 12:25 AM
Perfect Thomas, and thank You very much

: ) Happy Computing and Happy Christmas !

kennet

rkmcswain
2004-12-23, 02:12 PM
:?.....

....hmmmm, wondering what was wrong with the shorter solution posted 2 days ago...



(defun Hex2Dec (stg / cnt len dgt val lst)
(setq cnt 1
len (strlen stg)
)
(repeat len
(setq dgt (strcase (substr stg cnt 1))
val (if (> (ascii dgt) 64)
(- (ascii dgt) 55)
(atoi dgt)
)
lst (cons (* val (expt 16 (- len cnt))) lst)
cnt (1+ cnt)
)
)
(apply '+ lst)
)

kennet.sjoberg
2004-12-26, 06:59 PM
...hmmm2 rkmcswain, Your code is perfect to, but Your post did not show up for me until later on.
Tank You anyway

: ) Happy Computing!

kennet

rkmcswain
2004-12-27, 01:23 PM
It was in the original post. You had to follow the posted URL.

http://groups-beta.google.com/group...2aad500cd5bfdc9

CADmium
2004-12-29, 05:15 PM
:?.....

....hmmmm, wondering what was wrong with the shorter solution posted 2 days ago...


.. its's works also , but i was looking for a solution, that also works with other bases ...
for instance (DT:StrBaseX2INT "1011110" 2) ....