Hex encoding converts binary data into a hexadecimal representation, making the binary data more human-readable. In contrast, Hex decoding converts a hexadecimal string back into its original binary form. This tutorial explains how to perform Hex encoding and decoding on Linux.
Encoding string
The xxd
command is a versatile tool for performing Hex encoding and decoding. To encode a string, you can use the following approach:
echo 'Hello world' | xxd -p
Decoding string
To perform Hex decoding, use the -r
option.
echo '48656c6c6f20776f726c640a' | xxd -p -r
Encoding file
Create a text file for testing:
echo 'Hello world' > data.txt
Encode the contents of a text file and display the result in the terminal:
xxd -p data.txt
Encode the contents of a text file and save the result to another file:
xxd -p data.txt out.txt
Decoding file
Create a text file containing Hex encoded data:
echo '48656c6c6f20776f726c640a' > encoded_data.txt
Decode the contents of a text file and display the result in the terminal:
xxd -p -r encoded_data.txt
Decode the contents of a text file and save the result to another file:
xxd -p -r encoded_data.txt out.txt
Leave a Comment
Cancel reply