Search code examples
emacsnumber-formatting

Unibyte text buffers in Emacs: encode in hexa?


I have a "text" file that has some invalid byte sequences. Emacs renders these as "\340\360", is there a way to make the mighty text processor render those in hexadecimal, for instance, e.g.: "\co0a"?


Solution

  • Found it, just in case someone needs it too... (from here)

        (setq standard-display-table (make-display-table)) 
    (let ( (i ?\x80) hex hi low ) 
      (while (<= i ?\xff) 
        (setq hex (format "%x" i)) 
        (setq hi (elt hex 0)) 
        (setq low (elt hex 1)) 
        (aset standard-display-table (unibyte-char-to-multibyte i) 
              (vector (make-glyph-code ?\\ 'escape-glyph) 
                      (make-glyph-code ?x 'escape-glyph) 
                      (make-glyph-code hi 'escape-glyph) 
                      (make-glyph-code low 'escape-glyph))) 
        (setq i (+ i 1))))