Reputation: 2164
I'm trying to convert hexadecimal number to hexadecimal number presentation.
for example is below.
CL-USER> (foo 0D)
#x0D
Should i use macro function?
Upvotes: 1
Views: 1528
Reputation: 453
As phrased your question makes no sense. There is no such thing as a "hexadecimal number"; "hexadecimal" is the name of a number-presentation system. In your example, the input appears to be a symbol, as Rainer says, but not every such "hexadecimal" is a symbol. Presumably you want the output of (foo 10) to be #x10, and here the input is a number. So if the input is interpreted by the Lisp reader as a (base-10) number, then it should be converted to the number you would get if reread base 16.
Another point that Rainer overlooked is that if 0D is understood as a symbol, then (foo 0D) would cause an UNBOUND-VARIABLE error. You would have to write (foo '0D) to make 0D an input to foo.
On the output side, I don't think there is any native Lisp object that prints as "#x..." . If you want numbers to print in base 16, then you bind print-base to 16, but you won't see any sharpsign-x in front of it.
That suggests a somewhat different approach to your problem. We define a new datatype that does print starting with #x, and have foo construct such an object:
(defstruct (pseudo-hex (:print-object pseudo-hex-print))
thing)
(defun pseudo-hex-print (h srm)
(format srm "#x~a" (pseudo-hex-thing h)))
(defun foo (x)
(make-pseudo-hex :thing x))
Now we get the following:
cl-user(162): (foo 38)
#x38
cl-user(163): (foo '0D)
#x0D
I have a funny feeling this is not what Rafael was asking!
Upvotes: 0
Reputation: 139411
0D is a symbol (by default). Get its name with SYMBOL-NAME as a string. Use PARSE-INTEGER with :RADIX 16 to get the number.
If you don't have any reason for it, representing hex numbers as symbols is nothing I would do.
Upvotes: 3
Reputation: 86818
Are you saying that you want to format a number as a hexadecimal string? If so, you would want something like this: (format nil "~x" #x0D)
Upvotes: 0