Parse a hexadecimal number from a string, at some offset.
(parse-hex-from-string x val len n xl) → (mv val len)
This function is flexible but very complicated. See strval16 for a very simple alternative that may do what you want.
The final
Because of leading zeroes, the
See also parse-hex-from-charlist for a simpler function that reads a number from the start of a character list. This function also serves as part of our logical definition.
Function:
(defun parse-hex-from-string (x val len n xl) (declare (xargs :guard (and (stringp x) (natp val) (natp len) (natp n) (eql xl (length x))))) (declare (type string x) (type unsigned-byte val len n xl)) (declare (xargs :split-types t :guard (<= n xl))) (let ((acl2::__function__ 'parse-hex-from-string)) (declare (ignorable acl2::__function__)) (mbe :logic (b* (((mv val len ?rest) (parse-hex-from-charlist (nthcdr n (explode x)) val len))) (mv val len)) :exec (b* (((when (eql n xl)) (mv val len)) ((the character char) (char (the string x) (the unsigned-byte n))) ((when (hex-digit-char-p char)) (parse-hex-from-string (the string x) (the unsigned-byte (+ (the unsigned-byte (hex-digit-char-value char)) (the unsigned-byte (ash (the unsigned-byte val) 4)))) (the unsigned-byte (+ 1 (the unsigned-byte len))) (the unsigned-byte (+ 1 n)) (the unsigned-byte xl)))) (mv val len)))))
Theorem:
(defthm natp-of-parse-hex-from-string.val (b* (((mv ?val acl2::?len) (parse-hex-from-string x val len n xl))) (natp val)) :rule-classes :type-prescription)
Theorem:
(defthm natp-of-parse-hex-from-string.len (b* (((mv ?val acl2::?len) (parse-hex-from-string x val len n xl))) (natp len)) :rule-classes :type-prescription)