Here's an iterative solution
(defun decode-hex-string (hex-string) (let ((res nil)) (dotimes (i (/ (length hex-string) 2) (apply #'concat (reverse res))) (let ((hex-byte (substring hex-string (* 2 i) (* 2 (+ i 1))))) (push (format "%c" (string-to-number hex-byte 16)) res)))))
And using loop if you want to avoid side effects (you might need (require 'cl) to use this):
(defun decode-hex-string (hex-string) (apply #'concat (loop for i from 0 to (- (/ (length hex-string) 2) 1) for hex-byte = (substring hex-string (* 2 i) (* 2 (+ i 1))) collect (format "%c" (string-to-number hex-byte 16)))))
In general, it is better to avoid recursion in Elisp and Common Lisp; your stack will keel with a sufficiently large amount of input, and none of the languages guarantees tail recursion (which you do not use, but still). In Scheme, this is a different story.
By the way, Happy 39th.
source share