Convert a text file with hexadecimal values to a binary file

Overview

--Hexary text file-> Convert to binary file

test.txt


1234567890abcdef

↓ Binary file test.bin test.bin.png

For python3.2 or later in this example, use int.to_bytes

python


with open("test.txt") as fin,  open("test.bin", "wb") as fout:
    s = fin.read()
    fout.write(int(s,16).to_bytes(len(s)//2,"big"))

It's easy to do.

The following is the code when it corresponds even if there are spaces or line breaks.

code

python


import re
with open('test.txt', 'r') as fin:
  s = fin.read()
  s = re.sub(r'[\r\n\t ]|0x', '', s)
  bins = [int(a+b,16) for a, b in zip(s[::2], s[1::2])]
  with open('test.bin', 'wb') as fout:
     fout.write(bytearray(bins))

s = re.sub(r'[\r\n\t ]|0x', '', s) Line breaks / tabs / half-width spaces and 0x are deleted (replaced).

bins = [int(a+b,16) for a, b in zip(s[::2], s[1::2])] The pairs of a and b are taken out to form a hexadecimal number and put in the list.

If you show for a, b in zip (s [:: 2], s [1:: 2]) in a little more detail, it works as follows.

In [59]: s = "1234567890abcdef"
    ...: for a, b in zip(s[::2], s[1::2]):
    ...:     print(f"a={a}, b={b}")
a=1, b=2
a=3, b=4
a=5, b=6
a=7, b=8
a=9, b=0
a=a, b=b
a=c, b=d
a=e, b=f

Conversion example 1

As described in the outline

Conversion example 2

test2.txt


0x12 0x34 0x56 0x78 0x90

test2.png

Conversion example 3

test3.txt


1234
56789
abc
def

test3.png

Another method

Search Stack Overflow by referring to @ shiracamu's method in python2. To sum up the answers, it seems that python3 can be written as follows using codecs.

with_codecs.py


import codecs
with open('test.txt', 'r') as fin, open('test.bin', 'wb') as fout:
  s = re.sub(r'[\r\n\t ]|0x', '', fin.read())
  fout.write(bytearray(codecs.getdecoder("hex_codec")(s)[0])

reference

-[Handling hexadecimal numbers in Python-Introduction to Python] 1 -[[Python] Divide the character string into two characters and store it in the list --Bagbag World] 2 --[Create a binary file in Python --Qiita] 3

Recommended Posts

Convert a text file with hexadecimal values to a binary file
Convert hexadecimal string to binary
Convert HTML to text file
Create a large text file with shellscript
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
How to read a CSV file with Python 2/3
Speaking Japanese with OpenJtalk (reading a text file)
Speaking Japanese with gTTS (reading a text file)
Convert svg file to png / ico with Python
Convert Windows epoch values to date with python
Save the object to a file with pickle
I want to write to a file with Python
Convert 202003 to 2020-03 with pandas
I want to display only different lines of a text file with diff
How to convert / restore a string with [] in python
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
How to convert a class object to a dictionary with SQLAlchemy
How to convert JSON file to CSV file with Python Pandas
How to convert a mel spectrogram back to a wav file
Output a binary dump in binary and revert to a binary file
Upload a file to Dropbox
Export a gzip-compressed text file
Convert genbank file to gff file
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to convert an array to a dictionary with Python [Application]
[Caffe] Convert mean file from binary proto format to npy format
Convert Excel file to text in Python for diff purposes
Convert .ipynb to .html (with BatchFile)
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
I tried to convert a Python file to EXE (Recursion error supported)
[Python] Write to csv file with Python
Convert A4 PDF to A3 every 2 pages
Make it possible to output a log to a file with go echo
Build a deb file with Docker
Output to csv file with Python
What to do if you grep a text file and it becomes Binary file (standard input) matches
Write standard output to a file
How to make a command to read the configuration file with pyramid
Diff a binary file (image file, etc.)
Create a binary file in Python
Convert list to DataFrame with python
Output cell to file with Colaboratory
Convert sentences to vectors with gensim
I want to do a full text search with elasticsearch + python
Convert a large number of PDF files to text files using pdfminer
Download Google logo → Convert to text with OCR → Display on HTML
Convert a string to an image
Convert PDF to image with ImageMagick
A tool to convert Juniper config
[ROS2] How to play a bag file with python format launch
Try adding a wall to your IFC file with IfcOpenShell python
How to create a config file
Create a file uploader with Django
[Python] How to extract / delete / convert a matrix containing missing values (NaN)
Extract lines that match the conditions from a text file with python
I made a tool to convert Jupyter py to ipynb with VS Code
Read CSV file with Python and convert it to DataFrame as it is
[Python] How to convert db file to csv
Creating a simple PowerPoint file with Python
English speech recognition with python [speech to text]
Convert memo at once with Python 2to3