1

How do you convert a list into a string? I am trying to use parse-int to take a list of numbers and convert them to decimal, but i end up getting an error saying "The control-string must be a string, not (contents)". I'm using format, but I'm not sure if I'm using it incorrectly.

Here's my code:

    (princ "Enter a or list of hexadecimal numbers: ")
    (setq numList (read-from-string (concatenate 'string "(" (read-line) ")")))
    (defun hextodec(nums)
            (setq  numString (format "%s" nums))
            (setq newNum (parse-integer numString :radix 16))
            (write nums)
            (princ " = ")
            (write newNum)
    ) ;This will format the number that the user enters
    (hextodec numList)
4
  • 1
    "%s" is not valid in Lisp. You also use a lot of undeclared variables like numList, numString, newNum. You also miss to clear the output buffer after printing. See FINISH-OUTPUT. Commented Dec 6, 2017 at 7:45
  • The basic string printing directive is ~a (aesthetics). basic string formatting. Commented Dec 6, 2017 at 11:02
  • Maybe you are accustomed to Clojure's format? As others have noted, Lisp format (and the Clojure cl-format function from the pretty-print module) would be (format nil "~s"). On a different issue, it seems hextodec thinks its input is a list in one place and a single hex string in another place. Commented Dec 8, 2017 at 18:39
  • @RainerJoswig "%s" is not valid in Common Lisp's format, but it's valid in Emacs Lisp's format. However, this is tagged common-lisp, so that seems rather moot. Commented Dec 12, 2017 at 2:08

2 Answers 2

1

Since you're using read-from-string anyway, you can just tell Lisp's reader to read base 16 integers:

;; CLISP session
[1]> (let ((*read-base* 16)) (read-from-string "(9 A B C F 10)"))
(9 10 11 12 15 16) ;
14

read-from-string is a potential security hole if the contents of the string are untrusted input, because of the hash-dot evaluation notation.

When using the Lisp reader on untrusted data, be sure bind *read-eval* to nil.

[2]> (read-from-string "(#.(+ 2 2))")
(4) ;
11

Note how the #. notation causes the + function specified in the string data to be executed. That could be any function, such as something that whacks files in your filesystem, or sends sensitive data to a remote server.

Sign up to request clarification or add additional context in comments.

Comments

0

Format's first argument is the stream to print to. You seem to intend to return the string instead of printing, so you should put nil there: (format nil "~s" nums)

The format control string "%s" does not contain any format directives. The entire format form does not make much sense here, as you seem to intend to loop over the given nums instead. You should employ some looping construct for that, e. g. loop, do, map, mapcar ….

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.