Convert a String of Hex Values to its Binary Representation Using sed

Suggest you have data in a hex format that you want to convert back to binary (like an MBR) with newline characters in the middle:

eb58904d53444f53352e300002082200
0200000000f800003f00ff0000000000
0000f000e33b00000000000002000000
01000600000000000000000000000000
00002918d163804e4f204e414d452020
2020464154333220202033c98ed1bcf4
7b8ec18ed9bd007c884e028a5640b408
cd137305b9ffff8af1660fb6c640660f
b6d180e23ff7e286cdc0ed0641660fb7
c966f7e1668946f8837e16007538837e
2a007732668b461c6683c00cbb0080b9
0100e82b00e94803a0fa7db47d8bf0ac
84c074173cff7409b40ebb0700cd10eb
eea0fb7debe5a0f97debe098cd16cd19
6660663b46f80f824a00666a00665006
53666810000100807e02000f852000b4
41bbaa558a5640cd130f821c0081fb55
aa0f851400f6c1010f840d00fe4602b4
428a56408bf4cd13b0f9665866586658
6658eb2a6633d2660fb74e1866f7f1fe
c28aca668bd066c1ea10f7761a86d68a
56408ae8c0e4060accb80102cd136661
0f8254ff81c300026640490f8571ffc3
4e544c44522020202020200000000000
00000000000000000000000000000000
00000000000000000000000000000000
0000000000000000000000000d0a5265
6d6f7665206469736b73206f72206f74
686572206d656469612eff0d0a446973
6b206572726f72ff0d0a507265737320
616e79206b657920746f207265737461
72740d0a0000000000accbd8000055aa

Like this you can convert it back to its binary content:

cat datafile.txt | echo -n -e $(tr -d '[:space:]' | sed 's/../\\x&/g')

Resources

Comments