• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
      • Std/lists
      • Std/alists
      • Obags
      • Std/util
      • Std/strings
        • Pretty-printing
        • Printtree
        • Base64
        • Charset-p
        • Strtok!
        • Cases
        • Concatenation
        • Html-encoding
        • Character-kinds
        • Substrings
        • Strtok
        • Equivalences
        • Url-encoding
        • Lines
        • Explode-implode-equalities
        • Ordering
        • Numbers
          • Decimal
          • Hex
            • Parse-hex-from-string
            • Hex-digit-char-p
            • Nat-to-hex-chars
            • Parse-hex-from-charlist
            • Hex-digit-chars-value
            • Hex-digit-char-value
            • Take-leading-hex-digit-chars
            • Hexify
            • Hex-digit-char-listp
            • Hex-digit-char-list*p
            • Hex-digit-string-p
            • Strval16
            • Skip-leading-hex-digits
            • Nat-to-hex-string
            • Hexify-width
              • Nonzero-hex-digit-char-p
              • Nat-to-hex-string-list
              • Revappend-nat-to-hex-chars
              • Hex-digit-to-char
              • Nat-to-hex-string-size
            • Octal
            • Binary
          • Pad-trim
          • Coercion
          • Std/strings/digit-to-char
          • Substitution
          • Symbols
        • Std/osets
        • Std/io
        • Std/basic
        • Std/system
        • Std/typed-lists
        • Std/bitsets
        • Std/testing
        • Std/typed-alists
        • Std/stobjs
      • Proof-automation
      • Macro-libraries
      • ACL2
      • Interfacing-tools
      • Hardware-verification
      • Software-verification
      • Math
      • Testing-utilities
    • Hex

    Hexify-width

    Convert numbers into readable hex strings, fixing the number of digits printed

    (hexify-width x width) produces output just like hexify, but when printing an integer it always prints the given number of digits. When the number to be printed is longer, it truncates the more significant digits, and when it is shorter, it pads with 0s.

    ACL2 !> (cw "foo is ~s0~%" (str::hexify-width (1- (expt 2 32)) 12))
    foo is #ux0000_FFFF_FFFF
    NIL
    ACL2 !>

    The #ux is for compatibility with the ACL2::sharp-u-reader.

    Definitions and Theorems

    Function: hexify-width

    (defun hexify-width (x width)
     (declare (xargs :guard (posp width)))
     (b* ((width (mbe :logic (if (posp width) width 1)
                      :exec width)))
      (cond
       ((integerp x)
        (b*
         ((xsign (< x 0))
          (xabs (abs x))
          (chars (explode-atom xabs 16))
          (fixed-chars (cond ((<= width (len chars))
                              (nthcdr (- (len chars) width) chars))
                             (t (append (make-list (- width (len chars))
                                                   :initial-element #\0)
                                        chars))))
          (nice-chars
           (list*
             #\# #\u #\x
             (append
                  (and xsign '(#\-))
                  (cons (first fixed-chars)
                        (insert-underscores (nthcdr 1 fixed-chars)))))))
         (implode nice-chars)))
       ((symbolp x) (symbol-name x))
       ((stringp x) x)
       (t (prog2$ (er hard? 'hexify-width
                      "Unexpected argument ~x0.~%" x)
                  "")))))