In Linux, the “xxd” command is a powerful tool that works with binary data. It is utilized for various purposes such as creating binary files, displaying the contents of binary files, and converting between different data representations. However, the xxd is not designed to output the binary representation of a hex number, because its input is a binary file or standard input, not a hexadecimal number.
This guide will demonstrate the usage of the “xxd” command to output the binary representation of hex numbers in Linux.
How to Output the Binary Representation of a Hex Number Using xxd Command?
The “xxd” command is utilized to create a hex dump (hexadecimal representation) of a given input. To display the binary format of a hex number, use the “xxd” command. The basic syntax is mentioned below:
$ echo -n "HEX_NUMBER" | xxd -r -p | xxd -b
The description of the above syntax is given below:
- “HEX_NUMBER“: is the hex number that users want to convert to binary.
- –n: It is utilized to suppress the trailing newline character.
- xxd: This command reads the hex number from standard input, converts it to binary data, and outputs the binary data to standard output.
- –r: Option tells xxd to reverse the conversion process (from hex to binary instead of binary to hex),
- –p: Tells to interpret the input as a plain hex dump (without any formatting or line breaks).
- -b: The option tells xxd to use the binary (base 2) output format instead of the default hex (base 16) format.
By combining these commands, users can easily convert a hex number to its binary representation using the “xxd” command in Linux.
To convert the hex number to binary representation, execute the “xxd” command with the “r” and “p” options by specifying the hexadecimal number “ABCD”:
$ echo -n "ABCD" | xxd -r -p | xxd -b
The output of the command is the binary representation “10101011 11001101” of the hex number “ABCD“. Each byte of the binary representation is displayed as a sequence of 8 bits.
To output, the binary representation of a hex number, use the -b option. For instance, the binary representation of the hex number 7b is written below:
$ echo 7b | xxd -b
The output displays the binary number of a given “7b” number.
To explore the “xxd” command in Linux, follow our article “xxd Command in Linux”.
To output the binary representation of a hex number, execute the “echo -n “<hex_number>” | xxd -r -p | xxd -b” command in Linux. It reads the hex number from standard input, converts it to binary data, and outputs the binary data to standard output.
This article has explained how to output the binary representation of a hex number using the hex number.