home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / gnu / emacs / help / 4837 < prev    next >
Encoding:
Text File  |  1992-11-17  |  2.2 KB  |  70 lines

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!bu.edu!olivea!charnel!rat!usc!zaphod.mps.ohio-state.edu!uwm.edu!news.bbn.com!noc.near.net!mv!world!jrs
  2. From: jrs@world.std.com (Rick Sladkey)
  3. Newsgroups: gnu.emacs.help
  4. Subject: Re: hexlify-buffer
  5. Message-ID: <JRS.92Nov17212206@lepton.world.std.com>
  6. Date: 18 Nov 92 02:22:05 GMT
  7. References: <m0mpyAL-0001BGC@fly.cnuce.cnr.IT>
  8. Sender: jrs@world.std.com (Rick Sladkey)
  9. Organization: The Internet
  10. Lines: 57
  11. In-Reply-To: pot@fly.cnuce.cnr.it's message of 13 Nov 92 12:22:00 GMT
  12.  
  13. >>>>> On 13 Nov 92 12:22:00 GMT, pot@fly.cnuce.cnr.it (Francesco
  14. >>>>> Potorti`) said:
  15.  
  16. Francesco> Again, anyone able to make it faster?
  17.  
  18. I was also experimenting with hexlify-buffer and ended up doing it
  19. this way.  On my system it is about six times faster than the original
  20. and about twice as fast as Francesco's version.
  21. -----
  22. (defvar char-to-hex-vector (make-vector 256 nil)
  23.   "Vector of hex string equivalents of characters.")
  24.  
  25. (let ((i 0))
  26.   (while (< i 256)
  27.     (aset char-to-hex-vector i (format "%02x" i))
  28.     (setq i (1+ i))))
  29.  
  30. (defvar char-to-prt-vector (make-vector 256 nil)
  31.   "Vector of printable string equivalents of characters.")
  32.  
  33. (let ((i 0))
  34.   (while (< i 256)
  35.     (aset char-to-prt-vector i
  36.       (format "%c" (if (or (< i 32) (>= i 127)) 46 i)))
  37.     (setq i (1+ i))))
  38.  
  39. (defun hexlify-buffer ()
  40.   "Convert a binary buffer to hexl format"
  41.   (interactive)
  42.   (goto-char (point-min))
  43.   (let ((address 0))
  44.     (while (not (eobp))
  45.       (let* ((i (min (- (point-max) (point)) 16))
  46.          (line (append (buffer-substring (point) (+ (point) i)) nil))
  47.          (hex nil)
  48.          (prt nil))
  49.     (while line
  50.       (setq hex (cons (aref char-to-hex-vector (car line)) hex))
  51.       (setq prt (cons (aref char-to-prt-vector (car line)) prt))
  52.       (setq line (cdr line))
  53.       (if line
  54.           (progn
  55.         (setq hex (cons (aref char-to-hex-vector (car line)) hex))
  56.         (setq hex (cons " " hex))
  57.         (setq prt (cons (aref char-to-prt-vector (car line)) prt))
  58.         (setq line (cdr line)))))
  59.     (delete-region (point) (+ (point) i))
  60.     (insert (format "%08x: " address))
  61.     (apply (function insert) (nreverse hex))
  62.     (insert-char 32 (if (= i 16) 1
  63.               (+ (* (- 8 (/ i 2)) 5) 1 (* -2 (% i 2)))))
  64.     (apply (function insert) (nreverse prt))
  65.     (insert "\n")
  66.     (setq address (+ address 16))))))
  67. --
  68. Rick Sladkey
  69. jrs@world.std.com
  70.