GerhardHochholzer (talk | contribs) No edit summary |
JohnBeckett (talk | contribs) (→Comments: thanks; what now?) |
||
Line 94: | Line 94: | ||
</pre> |
</pre> |
||
[[User:GerhardHochholzer|GerhardHochholzer]] 12:36, 4 February 2009 (UTC) |
[[User:GerhardHochholzer|GerhardHochholzer]] 12:36, 4 February 2009 (UTC) |
||
+ | |||
+ | :Oops -- I forgot to read the docs! Thanks Gerhard. So what should we do with this tip? Perhaps replace its content (remove the script) with a short statement of wanted outcome, and a couple of examples (yours and <tt>:r !xxd -i file.bin</tt>)? --[[User:JohnBeckett|JohnBeckett]] 04:27, 5 February 2009 (UTC) |
Revision as of 04:27, 5 February 2009
Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).
created January 14, 2009 · complexity basic · version 7.0
This function turns an xxd hexdump into the equivalent C source code.
Source the following script, or put it in your vimrc:
function! Hexdump2C() s/^\x\+: // s/\s\s.*// s/\s//g s/\(..\)/\\x\1/g s/.\+/"&"/ endfunction
In Vim, if the buffer contains only output from xxd, enter the following to convert the whole buffer:
:%call Hexdump2C()
Alternatively, visually select the lines containing the xxd output, and enter:
:call Hexdump2C()
Example
A small bitmap was loaded in Vim. The command
:%!xxd
was executed resulting in the following:
0000000: 424d 9600 0000 0000 0000 7600 0000 2800 BM........v...(. 0000010: 0000 0100 0000 f8ff ffff 0100 0400 0000 ................ 0000020: 0000 2000 0000 0000 0000 0000 0000 0000 .. ............. 0000030: 0000 0000 0000 0000 0000 0000 8000 0080 ................ 0000040: 0000 0080 8000 8000 0000 8000 8000 8080 ................ 0000050: 0000 8080 8000 c0c0 c000 0000 ff00 00ff ................ 0000060: 0000 00ff ff00 ff00 0000 ff00 ff00 ffff ................ 0000070: 0000 ffff ff00 b000 0000 9000 0000 7000 ..............p. 0000080: 0000 0000 0000 b000 0000 9000 0000 7000 ..............p. 0000090: 0000 0000 0000 0d0a ........
Executing
:%call Hexdump2C()
results in the following:
"\x42\x4d\x96\x00\x00\x00\x00\x00\x00\x00\x76\x00\x00\x00\x28\x00" "\x00\x00\x01\x00\x00\x00\xf8\xff\xff\xff\x01\x00\x04\x00\x00\x00" "\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x80" "\x00\x00\x00\x80\x80\x00\x80\x00\x00\x00\x80\x00\x80\x00\x80\x80" "\x00\x00\x80\x80\x80\x00\xc0\xc0\xc0\x00\x00\x00\xff\x00\x00\xff" "\x00\x00\x00\xff\xff\x00\xff\x00\x00\x00\xff\x00\xff\x00\xff\xff" "\x00\x00\xff\xff\xff\x00\xb0\x00\x00\x00\x90\x00\x00\x00\x70\x00" "\x00\x00\x00\x00\x00\x00\xb0\x00\x00\x00\x90\x00\x00\x00\x70\x00" "\x00\x00\x00\x00\x00\x00\x0d\x0a"
Comments
- I'm trying to extend this to include "char *buf =" and finish with a semicolon. Executing "%s/\_.\+/char *buf = &;/" results in what I want, but the following prepends "char *buf" to every line. Why? Also, how can I put the semicolon before the last newline?
function! Hexdump2C() s/^\x\+: // s/\s\s.*// s/\s//g s/\(..\)/\\x\1/g s/.\+/"&"/ s/\_.\+/char *buf = &;/ endfunction
Could anyone help?
xxd itself can create output in C file style. Try the following:
:%!xxd -i %
GerhardHochholzer 12:36, 4 February 2009 (UTC)
- Oops -- I forgot to read the docs! Thanks Gerhard. So what should we do with this tip? Perhaps replace its content (remove the script) with a short statement of wanted outcome, and a couple of examples (yours and :r !xxd -i file.bin)? --JohnBeckett 04:27, 5 February 2009 (UTC)