Encode and decode hex with xxd
How xxd encodes text to hex and decodes it back, why it doesn't convert decimal numbers to hex, and when hex output is practical or plain text is better.
xxd is a command-line hex dump utility distributed with the Vim editor. It turns any input bytes into hexadecimal text, and its reverse mode turns that hex text back into the original bytes. This review explains what that transformation changes, where it is useful, and where it causes problems. It also addresses a common source of confusion behind searches for converting decimal to hex with xxd: xxd encodes bytes, not numbers.
Is xxd the right transformation for your use?
xxd is a good choice when you need to see or move the exact bytes of some data. Its main benefit is that the round trip is lossless: hex produced by xxd and decoded with xxd -r yields the same bytes you started with. The main tradeoff is that the result is no longer readable as language. A person, a search engine or a screen reader sees only a run of digits and the letters a to f.
- Good fits: inspecting hidden characters such as trailing newlines, carriage returns, byte order marks or non-breaking spaces
- Good fits: checking how a character is encoded in UTF-8
- Good fits: making small byte-level edits to a binary file, then converting back
- Good fits: pasting binary data through a channel that only accepts plain ASCII text
- Poor fits: anything a person is meant to read, search or quote
- Poor fits: hiding or protecting content, because hex is an encoding and not encryption; anyone can decode it
- Poor fits: converting a decimal number to its hexadecimal value, which needs printf or shell arithmetic instead
Plain text is preferable whenever the goal is communication rather than byte inspection. If you share hex at all, share the plain-text version alongside it.
What xxd actually changes
xxd replaces each input byte with two hexadecimal digits. This changes the stored characters completely: the output is a new piece of ASCII text, not a styled or visually altered version of the original. There is no font effect, mirroring or display trick involved. The meaning is recoverable only by decoding.
Encoding text to hex
The plain style, -p, prints only the hex digits. This form is the easiest to copy and to decode later. The examples in this article show expected output based on xxd's documented format; they are not results of a recorded test.
$ printf 'Hi' | xxd -p
4869
Without -p, xxd prints its default dump layout, shown below. Each line starts with a hexadecimal offset, followed by the bytes in groups of two (16 bytes per line), then a text column. In the text column, non-printable bytes appear as dots. The input here comes from echo, which appends a newline. That newline is the trailing 0a, shown as a dot in the text column. Use printf, or echo -n, when the newline should not be part of the data.
$ echo 'Hi' | xxd
00000000: 4869 0a Hi.
Several options adjust the layout. The -u option prints uppercase hex letters. The -c option sets how many bytes appear per line. In plain style, output wraps at 30 bytes (60 hex digits) per line by default.
Decoding hex back with xxd -r
In xxd, 'reverse' means reverting a hex dump to binary. It does not reverse the order of characters. To decode plain hex, combine -r with -p. According to the documentation, extra whitespace and line breaks are allowed anywhere in plain hex input, so wrapped or re-flowed hex still decodes correctly.
$ echo '4869' | xxd -r -p
Hi
$ xxd data.bin > data.hex # default dump
$ xxd -r data.hex > restored.bin
Reverting the default layout relies on its offsets and hex columns. Edit the hex digits rather than the text column if you want a change to take effect. One caution: when xxd -r is given an output file name instead of writing to standard output, it writes into that file without truncating it. Existing bytes beyond the decoded data can therefore remain. Redirecting to a new file, as in the example above, avoids this.
Decimal to hex: what xxd does and does not do
Piping a decimal number into xxd does not produce its hexadecimal value. xxd sees the characters '2', '5' and '5' plus a newline, and encodes those bytes.
$ echo 255 | xxd -p
3235350a
$ printf '%x\n' 255 # decimal number to hex
ff
$ printf '%d\n' 0xff # hex number to decimal
255
Decimal does matter for file offsets. The seek (-s) and length (-l) options accept decimal values. The offset column, however, is printed in hex: reading 16 bytes from byte 1024 with xxd -s 1024 -l 16 file.bin labels the line 00000400. Recent xxd releases add a -d option that shows offsets in decimal. Older installations may not have it, so check xxd -h on your system.
Multi-byte characters and emoji
xxd works on bytes, not characters. In UTF-8, one visible character can occupy several bytes, and two characters that look identical can have different byte sequences.
| Input | Code points | UTF-8 hex from xxd -p |
| é (precomposed) | U+00E9 | c3a9 |
| é (e plus combining acute accent) | U+0065 U+0301 | 65cc81 |
| 👍 | U+1F44D | f09f918d |
Decoding restores exactly the bytes that were encoded. A decomposed é therefore comes back decomposed, and an emoji comes back whole, as long as the hex is kept intact. Problems appear if you cut or edit hex in the middle of a multi-byte sequence: the decoded result becomes invalid UTF-8 and may display as replacement characters. The hex also reflects the input's actual encoding. The same text saved as UTF-16 produces entirely different bytes.
Readability and usability after copying or sharing
Hex output uses only ASCII digits, the letters a to f and, in the default layout, spaces, colons and dots. That makes it more likely to survive copying between applications than raw binary or unusual Unicode. Because xxd -r -p tolerates added whitespace and line breaks, reflowing by a chat app or editor should not stop it from decoding. Changes that insert or substitute other characters are a different matter, so decode a copy and compare it with the original before relying on a given destination. This review does not include tests of specific messaging apps, editors or platforms.
For accessibility, hex is effectively unreadable as content. Assistive technology can only present it as a string of letters and digits that carries no meaning for the listener. No specific screen reader was tested for this review. If hex appears in documentation or a post, describe in plain text what it represents.
Hex also defeats ordinary search and editing. Searching for a word will not match its hex form, and editing hex by hand risks breaking characters, as described above. The output is at least twice the size of the input, before line breaks or the default layout's offsets and text column are added. If the goal is only to move binary through a text-only channel, base64 is more compact, though equally unreadable.
Two gaps remain. Option availability varies between xxd versions, so confirm flags locally before scripting around them. Compatibility claims for any named destination need a scoped test of that destination. xxd itself runs locally and does not need a network connection. Any website or other tool that offers xxd-style conversion has to be evaluated separately, and its privacy practices cannot be judged from how it looks or describes itself.