Use xxd to convert to hex
If you want to see the hex values of a file, the easiest way to do this is to use xxd.
Given foo.txt that contains “This is some text!”:
$ xxd foo.txt 00000000: 5468 6973 2069 7320 736f 6d65 2074 6578 This is some tex 00000010: 7421 0a t!.
There are three columns in the output:
- number of first character in hex
- Hex representation of up to 16 bytes, separated every 2 bytes
- ASCII representation, with a . for escape characters, like the 0A on the second line
There are some options that I find useful.
Firstly, we can use -u to get uppercase hex letters which I find more “normal”.
$ xxd -u foo.txt 00000000: 5468 6973 2069 7320 736F 6D65 2074 6578 This is some tex 00000010: 7421 0A t!.
More interestingly, -R always will colourise the output:
I particularly like how the colourised output has a different colour for the new line character so you can see that it isn’t a full stop (period for my US friends).
Lastly, -p will provide the plain dump style of just the hex characters:
$ xxd -u -p foo.txt 5468697320697320736F6D652074657874210A
This is quite handy when you don’t need all the clutter.
All in all, xxd is a lovely tool when you need to find out exactly which characters are in a string.
The best bit though is this warning in the man page:
WARNINGS
The tool's weirdness matches its creator's brain. Use entirely at your own risk. Copy files. Trace it. Become a wizard.
What more is there to say?