X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=lsp-misc%2Fmisc.lsp;h=9e77db93efe06db5c81e050265abcc7f3dd561b7;hb=de600b7667b5df4b1fb48c0c496f8e2fc86c08c7;hp=f60d0d3ea86fa7fe8ae2c5820bb426a89f958986;hpb=121167c737403e2f49231fd5704aae86850b5b38;p=rrq%2Flsp-utils.git diff --git a/lsp-misc/misc.lsp b/lsp-misc/misc.lsp index f60d0d3..9e77db9 100644 --- a/lsp-misc/misc.lsp +++ b/lsp-misc/misc.lsp @@ -6,11 +6,35 @@ (and N (exit N))) (global 'die) +(define (char2hex STR) + (join (map (curry format "%2x") (map char (explode STR))))) +(global 'char2hex) + ;; Print binary byte as octal or as ASCII character [32-126] (define (octal-byte x) (if (and (> x 31) (< x 127)) (char x) (format "\\%o" x))) +(global 'octal-byte) ;; Print string as binary octals (define (octals-string S) (join (map octal-byte (unpack (dup "b" (length S)) S)))) -(global 'octals-string 'octal-byte) +(global 'octals-string) + +;; Return byte code as printable or as code. +(define (human-byte B) + (if (and (> B 32) (< B 127)) (char B) B)) +(global 'human-byte) + +;; Return a packed encoding of a list of bytes, joining string elements +(define (human-bytes BL) + (let ((OUT '()) (X nil)) + (dolist (B (map human-byte BL)) + (if (string? B) (if X (extend X B) (setf X B)) + (begin (when (string? X) (push X OUT -1)) + (push B OUT -1) + (setf X nil)))) + (when (string? X) (push X OUT -1)) + OUT)) +(global 'human-bytes) + +"misc.lsp"