Wednesday, July 4, 2012

RE: How to write a text line as binary/Hex with a filename by means of Vim command line(s)?

William Fugh wrote:
> If there are 8 chars in one line like this:
> 52494646
> Question: using Vim command line(s), how to write a binary
> file, and make it 'RIFF' (4 char, not '52494646') in a viewer of TXT?

" Write a binary file (no newline at end) of characters translated
" from pairs of hex ASCII characters (no spaces) on current line.
function! WriteChars(outfile, text)
let chars = ''
for i in range(0, len(a:text)-1, 2)
let chars .= nr2char('0x'.a:text[i : i+1])
endfor
call writefile([chars], a:outfile, 'b')
endfunction
command! WriteChars call WriteChars('data.bin', getline('.'))

Put above in file writechars.vim.
In Vim, enter:
:so writechars.vim

Put cursor on wanted line and enter (type :Wr and press Tab):
:WriteChars

File data.bin now contains the four characters.

John

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: