You can use the search replace functionality of sed:

echo "00124a5b" | sed 's/../\\x&/g'
\x00\x12\x4a\x5b

The two dots search for any two characters in the stream. The \\x& replaces the match with a \x followed by the match. Adding the g on end tells sed to continue the search/replace.

I would check out this tutorial for sed: http://www.grymoire.com/Unix/Sed.html There are several other tutorials on that site for other helpful commands.

Answer from Patrick Glaser on Stack Overflow
Discussions

text processing - Convert hexadecimal to binary on Linux CLI - Unix & Linux Stack Exchange
I have a text file with lines (each of 631 characters) consisting of 16 hexadecimal numbers 0,1,2,...,9,A,B,...,F. I would like to replace each number with the corresponding 4 bits of 0 and 1, i.e. 0 More on unix.stackexchange.com
🌐 unix.stackexchange.com
April 27, 2016
shell - Linux script to convert byte data into a hex string - Stack Overflow
I have byte data, 0Y0*†HÎ*†HÎ=B¬`9E>ÞÕ?ÐŽ·‹ñ6ì­Â‰&ÉÐL_cüsyxoú¢ † (¯-›E–¿9ÐÖ1ªÐ›¯o Now, I want to create one Linux script that will convert that data into a hex string. How can I do that? More on stackoverflow.com
🌐 stackoverflow.com
To Convert Hex to Byte and Then Base64 Encoding - Storage & SAN - Spiceworks Community
I am trying to convert the hexadecimal value(a file checksum) to byte and then to convert it to base64 encoding for the files received. For this I’m using run program component with the below command. xargs -n 1 digest -a md5 | xxd -r -p | openssl enc -base64 From reformat, Iam sending file ... More on community.spiceworks.com
🌐 community.spiceworks.com
0
July 19, 2019
c - Transform hexadecimal information to binary using a Linux command - Stack Overflow
I have this binary file on my Linux system... udit@udit-Dabba ~ $ cat file.enc Salted__s�bO�� More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › GregTonoski › 353d0d27650331044ba0a1ca170721fa
Convert a hexadecimal string to bytes. · GitHub
Convert a hexadecimal string to bytes. GitHub Gist: instantly share code, notes, and snippets.
🌐
Spiceworks
community.spiceworks.com › hardware & infrastructure › storage & san
To Convert Hex to Byte and Then Base64 Encoding - Storage & SAN - Spiceworks Community
July 19, 2019 - I am trying to convert the hexadecimal value(a file checksum) to byte and then to convert it to base64 encoding for the files received. For this I’m using run program component with the below command. xargs -n 1 digest -a md5 | xxd -r -p | openssl enc -base64 From reformat, Iam sending file names to run program and trying to calculate the same.
Find elsewhere
🌐
DEV Community
dev.to › carlyraejepsenstan › converting-hex-to-text-the-bash-way-1483
Converting Hex to Text - the Bash Way - DEV Community
March 18, 2022 - Let's start by pretending someone gave us some pure plain-text bytes, like this: 68656c6c6f20776f726c64 · What are we going to do to find the hidden message? First, we know that hexadecimal bytes are always 2 characters - you can verify this by using man ascii in your terminal and checking out the tables it shows.
🌐
SysTutorials
systutorials.com › docs › linux › man › 1-xxd
Xxd (1) Linux Manual Page - SysTutorials
May 19, 2026 - However, this is a rare situation and the use of `+’ is rarely needed. The author prefers to monitor the effect of xxd with strace(1) or truss(1), whenever -s is used. Print everything but the first three lines (hex 0x30 bytes) of file.
🌐
Linux Man Pages
man7.org › linux › man-pages › man1 › hexdump.1.html
hexdump(1) - Linux manual page
Please note that the usual C escape sequences are not interpreted by hexdump inside the color_units. OFFSET An offset or an offset range at which to check for a match. Please note that lone OFFSET_START uses the same value as END offset. Counters The default and supported byte counts for the conversion characters are as follows: %_c, %_p, %_u, %c One byte counts only.
🌐
Unix.com
unix.com › shell programming and scripting
binary/hex output - Shell Programming and Scripting - Unix Linux Community
January 14, 2018 - i want to output something like 2f 00 00 00 but i can't seem to escape characters like i'm used to in some programming languages, so with this: echo "/\0\0\0" >> outputfile i actually get 2f 5c 30 5c 30 5c 30 0a ie the \0 isn't giving me the 00 i need, and in addition it has got an extra couple of bytes on the end - 0a (return carriage?). obviously i'm going about this the wrong way - any suggestions? thanks.
🌐
Reddit
reddit.com › r/hacking › understanding bytes and hex
r/hacking on Reddit: Understanding bytes and hex
November 18, 2019 -

Ok so i am trying to understand this concept (I have many questions)

Bytes are 1s and 0s, so binary Binary can be translated to hexadecimal So then can bytes be 1s and 0s and hexadecimal, like any of them two, (not sure about that tho)

I was watching a pentesting video on a hackthebox machine, and the guy was trying to change the so called magic bytes of a file so that the system recognized the file as a different file then it actually was, to do that he printed in plain text some hex stuff (which was the file signature in hex) like \xAb\xC3\xf3 etc etc into the file (echo \xAb\xC3\xF3 > file.txt) , so another question is when the computer read all these hex characters, did it import it in the bytes just like that, or did it recognize it has hex and then convert it to binary and then into the bytes

As you see i am very confused with bytes and how they store information, and how you can “insert” information in to bytes, and how all that would work. So if anybody could please explain like im 5 i would appreciate it a lot, as i am just starting to learn all of this stuff

Thank you!!

Ps: I hope you understand my question as I am not very good at explaining myself lol

Top answer
1 of 3
6
In traditional computing and networking all data is stored or transferred as binary. Binary is representative of an electrical state. When transferring data across the wire for example, a one is represented by a high (5v DC) and a zero is low (0v dc). You can think of this as on vs off. So it’s the same for magnetic media... the point on the platter is either in an on state (1) or off state (0). Hex is not what the data looks like in its storage state. Hex is a different method of “notation”. So I can take a byte and represent it in decimal notation like 10101010 or a shorten it by representing it as “aa”. Depending on the kernel and os or application, data is interpreted in many different ways. What the pen tester was doing was manipulating specific file header bytes. Which in their method could be “expressed” as hex. It would still be 1’s and 0’s at its lowest level as bits on disk. It’s most important that you under stand that depending on the layer that you are looking at data will be expressed in several different ways. The ascii character “a” is still bits on disk somewhere... but you don’t see it in decimal notation right? Hope that helps... Cheers!
2 of 3
4
1 bit is a 1 or a 0 so: 1 A byte is 8 bits so:, 11010101 I will come to hex later. So to understand binary you first need to understand decimal which u are using all you life. Decimal has a base of 10 which means each number is a power of 10 so how you calculate 123 is : So anything to the pow of 0 equals 1(except for 0 to the pow of 0) 3 * 10 to the pow of 0 = 3 * 1 = 3 2 * 10 to the pow of 1 = 2 * 10 = 20 1 * 10 to the pow of 2 = 1 * 10 *10 = 100 --> 3 + 20 + 100 = 123 Binary has a base of 2, let's take the example of my byte above: 11010101 The value for each bit from right to left therfore is 1, 2, 4, 8, 16, 32, 64, 128. A 1 means you use the value a 0 means you do not: 1 * 1 = 1 0 * 2 = 0 1 * 4 = 4 0 * 8 = 0 1 * 16 = 16 0 * 32 = 0 1 * 64 = 64 1 * 128 = 128 --> 1 + 4 + 16 + 64 + 128 =213 Now Hof hex values you have a base of 16, the numbers are from 0 - F 0123456789ABCDEF Usually written as 0x1A A 2 digit hex value can hold the same value as a byte(8 bits) that's why it is used so much, it is easier to read for humans and a computer reads it the same as a byte. The hex 0x1A has the value A * 1 = 10 1 * 16 = 16 --> 10 + 16 = 26 So computer turns every number into bits so it does not really matter if you give it a number in binary, decimal or hex it is just another way to read it. If you look for videos on something like "hex to Dec to bin" I am sure you will find some good videos. Hope this helps a bit, wrote it in a train on my phone so sry for typos.
🌐
Ubuntu
manpages.ubuntu.com › trusty › man(1)
Ubuntu Manpage: xxd - make a hexdump or do the reverse.
Copy input_file to output_file and prepend 100 bytes of value 0x00. % xxd input_file | xxd -r -s 100 > output_file · Patch the date in the file xxd.1 % echo "0000037: 3574 68" | xxd -r - xxd.1 % xxd -s 0x36 -l 13 -c 13 xxd.1 0000036: 3235 7468 204d 6179 2031 3939 36 25th May 1996 · Create a 65537 byte file with all bytes 0x00, except for the last one which is 'A' (hex 0x41).
🌐
Codingace
codingace.net › conversion › hex_to_bytes.html
Hex to Bytes Calculator
Press Calculate to view the converted byte table. Use CSV or PDF buttons to download the same validated result. Hexadecimal notation is a compact way to write binary data. Each hex character represents four bits. Two hex characters therefore create one byte. That byte can store a value from ...
🌐
Baeldung
baeldung.com › home › files › file conversion › convert binary data to hexadecimal
Convert Binary Data to Hexadecimal | Baeldung on Linux
June 14, 2022 - Then that is passed to the xxd command. We’ve used the xxd without any options. And it has printed the binary data in hex and ASCII format. Default output may not be the one we always need.