Tuesday, July 3, 2012

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

On Wed, Jul 04, 2012 at 12:18:48AM EDT, William Fugh wrote:

> -Vim 7.3 + Vista(SP2)
> 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?

Just to illustrate:

let l1 = [52, 49, 46, 46]
let l2 = map(l1, 'nr2char(v:val)')
let s1 = join(l2, '')
echo s1
41..
echo len(s1)
4

The s1 variable contains four hex values:

byte 0 is 0x34 .. ascii value of '4'
byte 1 is 0x31 .. ascii value of '1'
byte 2 is 0xe2 .. ascii value of '.'
byte 3 is 0xe2 .. ascii value of '.'

One problem is that if you copy the contents of the s1 variable to a Vim
buffer, Vim will add an eol (x0A) character to the string when you write
the buffer to a file.

$ od -x /tmp/myriff.txt

0000000 3134 2e2e 000a
0000005

Unless s/o else can figure it out from the above description, you may
want to explain your use case and why you need to use Vim to do this in
the first place.

There may be tools more suited to the job.

CJ

--
WE GET SIGNAL

--
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: