1

Select text in vim and convert it to a hex dump...

:'<,'>!xxd

(Note 2 hex characters represent a single letter...in whatever encoding I did this in...)

Select a hex dump in vim and convert it back to text...

:'<,'>!xxd -r

Select text in vim and convert it to a binary dump...

:'<,'>!xxd -b

How do you convert it back from the binary dump?

3
  • 1
    Does xxd -r or xxd -br work? I haven’t tried it, but... PS you might be interested in my plugin hex – D. Ben Knoble Jan 14 '20 at 23:35
  • 3
    xxd man page -b option says : The command line switches -r, -p, -i do not work with this mode. – dedowsdi Jan 15 '20 at 1:34
  • @dedowsdi unfortunate – D. Ben Knoble Jan 15 '20 at 13:43
-1

xxd version 1.10 ("xxd V1.10 27oct98 by Juergen Weigert (Win32)") states

xxd --help [...] Options: [...] -b binary digit dump (incompatible with -ps,-i,-r). Default hex.

@dedowsdi also pointed out the reverse operation with a binary dump does not work with xxd. It does not look like it can be done with xxd. Therefore I have come up with some alternative ways:

Option 1: vim dictionary-like method

I guess this could be call a dictionary method. Here are (I think) all of the non-control characters of ASCII without any tab character:

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

ASCII 32-126 (decimal) equals all of the characters on a QWERTY keyboard:

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Run this in vim (binary dump must match the /........ ........ ......../ type pattern with a space between each 8 binary numbers):

:%s/00100001/!/ge | %s/00100010/"/ge | %s/00100011/#/ge | %s/00100100/$/ge | %s/00100101/%/ge | %s/00100110/\&/ge | %s/00101100/,/ge | %s/00110010/2/ge | %s/00111000/8/ge | %s/00111110/>/ge | %s/01000100/D/ge | %s/01001010/J/ge | %s/01010000/P/ge | %s/01010110/V/ge | %s/01011100/\\/ge | %s/01100010/b/ge | %s/01101000/h/ge | %s/01101110/n/ge | %s/01110100/t/ge | %s/00100111/'/ge | %s/00101101/-/ge | %s/00110011/3/ge | %s/00111001/9/ge | %s/00111111/?/ge | %s/01000101/E/ge | %s/01001011/K/ge | %s/01010001/Q/ge | %s/01010111/W/ge | %s/01011101/]/ge | %s/01100011/c/ge | %s/01101001/i/ge | %s/01101111/o/ge | %s/01110101/u/ge | %s/00101000/(/ge | %s/00101110/./ge | %s/00110100/4/ge | %s/00111010/:/ge | %s/01000000/@/ge | %s/01000110/F/ge | %s/01001100/L/ge | %s/01010010/R/ge | %s/01011000/X/ge | %s/01011110/^/ge | %s/01100100/d/ge | %s/01101010/j/ge | %s/01110000/p/ge | %s/01110110/v/ge | %s/00101001/)/ge | %s/00101111/\//ge | %s/00110101/5/ge | %s/00111011/;/ge | %s/01000001/A/ge | %s/01000111/G/ge | %s/01001101/M/ge | %s/01010011/S/ge | %s/01011001/Y/ge | %s/01011111/_/ge | %s/01100101/e/ge | %s/01101011/k/ge | %s/01110001/q/ge | %s/01110111/w/ge | %s/00101010/*/ge | %s/00110000/0/ge | %s/00110110/6/ge | %s/00111100/</ge | %s/01000010/B/ge | %s/01001000/H/ge | %s/01001110/N/ge | %s/01010100/T/ge | %s/01011010/Z/ge | %s/01100000/`/ge | %s/01100110/f/ge | %s/01101100/l/ge | %s/01110010/r/ge | %s/01111000/x/ge | %s/00101011/+/ge | %s/00110001/1/ge | %s/00110111/7/ge | %s/00111101/=/ge | %s/01000011/C/ge | %s/01001001/I/ge | %s/01001111/O/ge | %s/01010101/U/ge | %s/01011011/[/ge | %s/01100001/a/ge | %s/01100111/g/ge | %s/01101101/m/ge | %s/01110011/s/ge | %s/01111001/y/ge | %s/01111010/z/ge | %s/01111011/{/ge | %s/01111100/\|/ge | %s/01111101/}/ge | %s/01111110/~/ge | %s/\s//ge | %s/00100000/ /ge

Option 2: use JavaScript and HTML

Render the following simple html file with your binary dump as a string in the binaryAgent function. It will work with all of ASCII, the tab character, and probably also extended ASCII and maybe Unicode.

<script>
//From
//https://stackoverflow.com/questions/21354235/converting-binary-to-text-using-javascript
function binaryAgent(str) {
  var binString = '';
  str.split(' ').map(function(bin) {
    binString += String.fromCharCode(parseInt(bin, 2));
  });
  return binString;
}
document.write(binaryAgent('01110010 01100111 00110010 00110011'));
</script>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.