Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to convert hex values to binary. For example, I have got some text file containing:

0.0 010111010  B4
0.1 001001011  A3
...

And I'm trying to convert B4 to 10110100, and A3 too.

But I can't find any method. So does anyone know how to do that?

share|improve this question
up vote 8 down vote accepted

The helpfile :h eval-examples contains an example of a number2binary function:

" The function Nr2Bin() returns the binary string representation of a number.
func Nr2Bin(nr)
  let n = a:nr
  let r = ""
  while n
    let r = '01'[n % 2] . r
    let n = n / 2
  endwhile
  return r
endfunc

Copy that example to your .vimrc and after restarting your vim, you can do :echo Nr2Bin(0xB4) and it will output 10110100.

When writing (e.g. in insert mode) you can then, press <C-R>=Nr2Bin(0xA3) and the result will be inserted into your buffer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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