Convert between hex and decimal

This tip shows how to convert numbers from hexadecimal to decimal, and the reverse. A variety of methods are given, including some simple commands you can enter, and some scripts to easily replace hex numbers with decimal, or to replace decimal with hex. Handling percent-encoded text is also discussed, and there are links to tips showing how to edit a binary file in hex.

Contents

Occasional conversions can be performed by entering commands. In Vim, put the cursor on 0x12345 and type :echo then Space Ctrl-R Ctrl-W Enter. That will execute :echo 0x12345 which displays 74565 (decimal equivalent of hex 12345).

An example of converting decimal to hex would be to enter :echo printf('%x',74565) which displays 12345 (hex equivalent of decimal 74565).

In addition, the expression register can be used to enter converted numbers while typing. In insert mode, press Ctrl-R then = then the expression wanted, then press Enter. The value of the expression is inserted, and you will remain in insert mode. For example, <C-R>=0x09ab<Enter> will insert 2475, and <C-R>=printf('0x%04x',2475)<Enter> will insert 0x09ab.

In normal mode, type ga to display the decimal and hex values for the character under the cursor, or type g8 to display the hex bytes for a UTF-8 character.

The following commands illustrate other simple techniques to convert strings to numbers which are displayed in decimal (the :echo is just for illustration; these techniques would be used in a script). The first shows that adding zero to a string converts the string to a number.

With the following mapping, you can put the cursor on a hex number and press \h (assuming the default backslash leader) to yank the value of the hex number (using :let means the equivalent decimal string is copied). You can then move the cursor elsewhere and press p to paste the decimal value.

nnoremap \h :let @@=<C-R><C-W><CR>

Alternatively, you can use a global variable to avoid using a register. With these mappings, \h will yank a hex value, and \p will paste the decimal string:

The above defines user commands :Dec2hex and :Hex2dec that will either display the result of converting a number that you enter, or will convert all numbers in selected text. A decimal number is a word consisting only of decimal digits, while a hex number consists of "0x" followed by one or more hex digits.

If no argument is entered with these commands, a range may be specified. The default range is the current line. A visual selection (character, line, or block) can also specify a range. When using a visual block (selection starts with Ctrl-V, or Ctrl-Q if Ctrl-V is used for paste), only numbers inside the block are converted. Example commands:

Command

Description

:Dec2hex 496

Displays 1f0 (hex equivalent of decimal 496).

:Dec2hex

Converts all decimal numbers to hex in current line.

:'<,>'Dec2hex

Same, for all visually selected text (v, V, or ^V).

:%Dec2hex

Same, for all lines in buffer.

:Hex2dec 0x1f0

Displays 496 (decimal equivalent of hex 1f0).

:Hex2dec 1f0

Same ("0x" is optional in an argument).

:Hex2dec

Converts all "0x" hex numbers to decimal in current line.

:'<,>'Hex2dec

Same, for all visually selected text (v, V, or ^V).

:%Hex2dec

Same, for all lines in buffer.

This version handles up to 32-bit long integers. An extended version of this script that works for arbitrary sized integers is available as a plugin on Github.

Sometimes one just needs to know the value of decimal/hexadecimal value, without changing the text.
Put the following in your vimrc to define user command 'gn' to print the number under the cursor converted to hex/decimal (similar to built-in command 'ga' and 'g8' for ASCII character under the cursor):

The following is from old tip 772. It does not seem helpful, but we should discuss the menus somewhere so I am keeping the text for a while.

This example creates a menu for conversions using the bc calculator which is a standard utility on many Unix-based systems. Visually select the number (without leading '0x'), right click the selection, and select from the popup menu to transform the number.