[LINUX] Output a binary dump in binary and revert to a binary file

I want to do something with just Linux commands!

Sometimes I wanted to display the dump result of a binary file in binary (only with the linux command). I will introduce what I did at that time. (There may be other good ways.)

First, prepare the following shell script.

hex2bin.sh


#!/bin/bash

hexdump -v -e '/1 "%02X\n"' $1 | awk '{print substr($1,1,1) " " substr($1,2,1)}' | awk -f hex2bin.awk 
Description

First, the first hexdump command outputs in 1-byte units.

hexdump -v -e '/1 "%02X\n"' $1

Output example

$ hexdump -v -e '/1 "%02X\n"' file
7F
45
4C
46
02
01
01
00
$

So, give this output to awk and put a space between them.

$ hexdump -v -e '/1 "%02X\n"' file | awk '{print substr($1,1,1) " " substr($1,2,1)}' 
7 F
4 5
4 C
4 6
0 2
0 1
0 1
0 0
$

I will give this output to awk again. Now specify the following script with the -f option.

hex2bin.awk


function hex2bin(shex) {
    bins["0"] = "0000";
    bins["1"] = "0001";
    bins["2"] = "0010";
    bins["3"] = "0011";
    bins["4"] = "0100";
    bins["5"] = "0101";
    bins["6"] = "0110";
    bins["7"] = "0111";
    bins["8"] = "1000";
    bins["9"] = "1001";
    bins["A"] = "1010";
    bins["B"] = "1011";
    bins["C"] = "1100";
    bins["D"] = "1101";
    bins["E"] = "1110";
    bins["F"] = "1111";

    return bins[shex];
}

{
    print hex2bin($1) " " hex2bin($2);
}

This will produce the following output.

sample.txt


0111 1111
0100 0101
0100 1100
0100 0110
0000 0010
0000 0001

Try to return the output result

Now that we've done this, let's convert it back to binary format with just the standard linux commands. I prepared the following awk script.

bin2hex.awk


function bin2hex(sbin) {
    hex["0000"] = "0";
    hex["0001"] = "1";
    hex["0010"] = "2";
    hex["0011"] = "3";
    hex["0100"] = "4";
    hex["0101"] = "5";
    hex["0110"] = "6";
    hex["0111"] = "7";
    hex["1000"] = "8";
    hex["1001"] = "9";
    hex["1010"] = "A";
    hex["1011"] = "B";
    hex["1100"] = "C";
    hex["1101"] = "D";
    hex["1110"] = "E";
    hex["1111"] = "F";

    return hex[sbin];
}

{
    printf "echo -en \"\\x%c%c\" >> hoge.bin\n",bin2hex($1),bin2hex($2)
}

with this

0111 1111

Is the character string

echo -en "\x7F" >> hoge.bin

Is converted to the echo command. As shown below, if you redirect to a neat file name and execute that file, a binary file will be output.

$ awk -f bin2hex.awk sample.txt > text2bin.sh
$ bash text2bin.sh

Recommended Posts

Output a binary dump in binary and revert to a binary file
Create a binary file in Python
Change the standard output destination to a file in Python
How to create a JSON file in Python
Output python log to both console and file
How to read a file in a different directory
Attempt to launch another .exe and save the console output to a text file
How to count the number of elements in Django and output to a template
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
[Python] Concatenate a List containing numbers and write it to an output file.
Output timing is incorrect when standard (error) output is converted to a file in Python
How to read a serial number file in a loop, process it, and graph it
Parse a JSON string written to a file in Python
Created a module to monitor file and URL updates
Convert a text file with hexadecimal values to a binary file
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
[Mac] A super-easy way to execute system commands in Python and output the results
Upload a file to Dropbox
Read and write a file
Write and read a file
How to output a document in pdf format with Sphinx
How to import a file anywhere you like in Python
A standard way to develop and distribute packages in Python
[Python] How to output a pandas table to an excel file
Just try to receive a webhook in ngrok and python
What to do if you cat or tail a binary file and the terminal is garbled
Count specific strings in a file
How to compare lists and retrieve common elements in a list
Save the binary file in Python
[Introduction to Python] How to output a character string in a Print statement
Process Splunk execution results using Python and save to a file
Output to csv file with Python
Diff a binary file (image file, etc.)
Output cell to file with Colaboratory
Read big endian binary in Python and convert it to ndarray
How to specify a .py file to load at startup in IPython 0.13
How to output the output result of the Linux man command to a file
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
I want to create a pipfile and reflect it in docker
Export and output files in Python
Python logging and dump to json
How to create a config file
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
How to input a character string in Python and output it as it is or in the opposite direction.
Replace the directory name and the file name in the directory together with a Linux command.
Create a shortcut to run a Python file in VScode on your terminal
How to get a specific column name and index name in pandas DataFrame
How to specify a .ui file in the dialog / widget GUI in PySide
How to put a half-width space before letters and numbers in Python.
Connect to postgreSQL from Python and use stored procedures in a loop.
How to make a container name a subdomain and make it accessible in Docker
The file name was bad in Python and I was addicted to import
I made a program to check the size of a file in Python
How to stop a program in python until a specific date and time
How to split and save a DataFrame
Data handling 1 Data formatting and file input / output
I want to print in a comprehension
Convert psd file to png in Python
Write O_SYNC file in C and Python