7

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?

8

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.

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.