Programmers use Hexadecimal or Hex numbers as a sort of bridge in understanding machine language in the form of binary with what most people know as readable language.
If you are unfamiliar with hexadecimal numbers, they are a type of numbering system with a base value of 16. And this means there are 16 unique symbols associated with hexadecimal, and they can represent any value possible.
Hexadecimal uses the numbers 0 to 9 and the letters a to f to represent numbers 10 to 16. This numbering system was developed to reduce large strings of binary numbers that need to be easily understood by humans.
What is the xxd Hex Dumper Utility Tool?
xxd is a command line utility tool that converts or dumps input files or strings into the hexadecimal number system. It can also convert hex to binary and revert the output hex into binary.
When dealing with debugging log files or texts that you need to convert to hexadecimal or vice versa, you may also need some handy utility tools to optimize your workflow. You can use screen and other helpful tools along the way.
How to install xxd
xxd command should already be natively installed in your Linux system. But in case you think it is still not yet installed in your system, you can install it by executing one of the commands you can see below:
To install xxd on a Debian-based OS, execute this command:
$ sudo apt install xxd
To install xxd on CentOS or other Fedora-based OS, input:
$ sudo dnf install xxd
Once installed, you can check what version was installed. Execute:
$ xxd --version
How to Use the xxd Hex Dumper Utility on Linux
In this article’s subsection, we will demonstrate how to use the screen utility command in converting your input file to the hexadecimal format and binary format. We will also be showing how to limit the displayed output and control which inputs will be converted.
Executing the help function: xxd -h[elp]
Converting files: xxd [options] [infile [outfile]]
Reverting converted output: xxd -r[evert] [options] [infile [outfile]]
If you’re curious to know more about the available flags you can use in xxd, you can visit the Official xxd Manpage.
Before we begin with the illustrations and examples of the
xxd command options, create a text file with alphanumeric words or phrases to keep track of the changes and understand how the command works. In this example, we have created a file named “input.txt”.
cat command to output what you’ve entered into the text file before executing the
$ cat input.txt
How to store the output to a file
To store the default output of xxd, simply pipe the output to your file. Follow this syntax:
$ xxd <inputfile> > <outputfile>
As discussed earlier, the hexadecimal output is grouped into four digits with a base of 16.
How to limit the output Hex Display
In this section, we can limit the output hex display depending on several conditions. Such as the desired length number in octets, the byte number, or the column length.
You can limit the number of convertible lines measured in octet length or bytes. To illustrate, use the following syntax:
$ xxd -l<number> <inputfile>
Notice how an octal value of 4 has the same effect as that of the decimal 4. In xxd, parameters to options can be specified in decimal, hexadecimal or octal notation. Thus -l6, -l 6, -c 060 are all equivalent.
Limit the column length
You can also limit the column lengths being converted by xxd. For example, execute the following command:
$ xxd -c 5 input.txt
How to print the last bytes of a file
To print the last bytes of a file, use the -s flag together with the
xxd command. Take for example, this syntax:
$ xxd -s <number> <inputfile>
As seen above, the -s flag starts from the last digit offsetting the last byte depending on the number supplied in the command.
How to convert the file to binary
xxd command also lets you convert an input string or file into the binary format system. Use the -b flag to dump the value. To illustrate, use the following command:
$ xxd -b input.txt
As a last-minute tip, you can also display the hex number system in uppercase. This is more pleasing visually, but not everyone may prefer this setup.
Affix the -u flag together with any other variable you might need.
You can also revert the dumped hex file into its original form. Take for example, our dumped output.txt file earlier. We can convert it to its original form by affixing the -r flag.
$ xxd -r output.txt
The Linux xxd utility tool can be useful when troubleshooting or debugging logs in hexadecimal format and analyzing the contents thereof. Systems administrators do not only use hexadecimal formats, but the whole IT community appreciates its compactness and simplicity.
Software developers also use them to convert long binary strings into a more condensed, easy-to -ead format. Programmers use them in microcontroller applications as well to compress signals into more manageable code.
We have demonstrated how to use the
xxd command through the Linux command line and provided different examples and illustrations to visualize its capabilities and functionalities.
If this guide helped you, please share it. ?