Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

decimal to hexadecimal

6 REPLIES 6
Reply
Message 1 of 7
markruys2
2819 Views, 6 Replies

decimal to hexadecimal

any functions which transform a decimal number to hexadecimal and vice versa?

thanks

6 REPLIES 6
Message 2 of 7
Kent1Cooper
in reply to: markruys2


@markruys2 wrote:

any functions which transform a decimal number to hexadecimal and vice versa?

....


You'll find lots of approaches if you search the forum for "hexadecimal," and if you like, add "handle" to the search terms, since entity handles are hexadecimal numbers, and various threads that have converters are about things like sorting drawing order by handles.

 

As part of some encompassing routines I posted on a thread called "making a block unique?" last August, here's a hex-to-integer converter I simplified somewhat from a more generic converter of Frank Oquendo's:

 

;; hex2int.lsp - Hexadecimal to base-10 Integer converter [e.g. for entity handles]
(defun hex2int (hndl / result power tmp)
  ;; modified from Frank Oquendo's (basetodecimal) function
  (setq
    hndl (strcase hndl)
    result 0
  ); end setq
  (repeat (setq power (strlen hndl))
    (setq result
      (+
        result
        (*
          (-
            (setq tmp (ascii (substr hndl 1 1)))
            (if (> tmp 64) 55 48)
          ); end -
          (expt 16 (setq power (1- power)))
        ); end *
      ); end +
      hndl (substr hndl 2)
    ); end setq
  ); end repeat
  result
); end defun - hex2int

 

Since that was about converting handles, there wasn't any need to go the other way, so I'd have to think about that.  If you don't find a decimal-to-hex converter elsewhere, write back.

Kent Cooper, AIA
Message 3 of 7
JustoAg
in reply to: markruys2

Take a look at these two old routines from Reini Urban if you don't find any better:

(defun STD-NUM->HEX	(i / s a)
	(setq s "")
	(while (> i 0)
		(setq a	(rem i 16)
			  i	(lsh i -4)
		) ;_  setq
		(setq s	(strcat
					(if	(< a 10)
						(chr (+ 48 a))	; 48: (ascii "0")
						(chr (+ 55 a))
					) ;_  if
					s
				) ;_  strcat
		) ;_  setq
	) ;_  while
) ;_  defun

(defun STD-HEX->NUM	(s / i n a)
	;(std-%simple-require "STDSTR")
	(setq i	0
		  n	0
		  ;s	(std-strcase s)
		  s	(strcase s)
	)
	(if	(= (substr s 1 2) "0X")
		(setq s (substr s 3))
	) ;_  if
	(while (< i (strlen s))
		(setq a (substr s (setq i (1+ i)) 1))
		(if	(not (<= "0" a "F"))
			(chr a)
		)
		(setq n	(+ (lsh n 4)
				   (- (ascii a)
					  (if (<= a "9")
						  48
						  55
					  ) ;_  if
				   ) ;_  -
				) ;_  +
		) ;_  setq
	) ;_  while
) ;_  defun

 

I think they still work. 

 

Message 4 of 7
markruys2
in reply to: markruys2

nice

thanks

m

Message 5 of 7
Kent1Cooper
in reply to: markruys2


@markruys2 wrote:

any functions which transform a decimal number to hexadecimal and vice versa?

....


I came up with the attached integer-to-hexadecimal converter, and modified that earlier hexadecimal-to-integer converter, so that both have input-validity controls, and the capacity to handle, for the integer-to-hex one, zero [which Reini Urban's STD-NUM->HEX routine that JustoAg posted doesn't like], and for both, negative numbers [which none of the routines posted here so far convert correctly].

 

Now when you say "decimal," do you really mean that -- that is, do you want to be able to convert non-integer values?  There is the possibility of decimal places in non-base-10 numbering systems, after all.  An AutoCAD entity handle would never have that, but that needn't be a limiting factor.  Another level of complexity, but it could be done, I suppose.

Kent Cooper, AIA
Message 6 of 7
markruys2
in reply to: Kent1Cooper

actually i need positive  integers only, but never know what the future brings...

thanks again

m

Message 7 of 7
LalaGhost
in reply to: markruys2

Hey I found some hexa to decimal converter with auto apply. It has som basic UI

 

https://gumroad.com/l/fdgG

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost