[PYTHON] Convert hexadecimal string to binary

Convert a hexadecimal string to binary and write it to a binary file

** [Method 1] Use to_bytes () ** Because you may want to change the endian Perform in the order of character string → int → bytes (binary).

str_to_bin.py


moji = '01020304'
suuchi = int(moji,16)

bytes_big = suuchi.to_bytes(4, byteorder='big')
bytes_little = suuchi.to_bytes(4, byteorder='little')

print(bytes_big)
print(bytes_little)

wf = open('write_test.bin', 'wb')
wf.write(bytes_big)
wf.write(bytes_little)

** print result **

16909060
b'\x01\x02\x03\x04'
b'\x04\x03\x02\x01'

** [Method 2] ** Perform in the order of character string → int → bytes (binary). Since to_bytes () can only be used with python3, use struct.

str_to_bin_2.py


import struct

moji = '01020304'
suuchi = int(moji,16)

bytes_big =struct.pack(">L",suuchi)
bytes_little =struct.pack("<L",suuchi)

** [Method 3] ** If you do not want to convert to endian, you can convert from character string to bytes (binary) below.

str_to_bin_3.py


moji = '01020304'
moji_bin = binascii.unhexlify(moji)

Reference link below

[Handling hexadecimal numbers in Python](http://kaworu.jpn.org/python/Python%E3%81%A716%E9%80%B2%E6%95%B0%E3%82%92%E6%89 % B1% E3% 81% 86) Python | 2's complement (signed) int ⇔ bytes ⇔ str (hexadecimal string) Numeric-> Binary-> Hexadecimal Strings-> Binary-> Convert to Numeric in Python.

[7.1. struct — Interpret byte sequence as packed binary data] (https://docs.python.jp/3/library/struct.html) 19.8. binascii — Conversion between binary data and ASCII data

Recommended Posts

Convert hexadecimal string to binary
[python] Convert date to string
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
Convert a text file with hexadecimal values to a binary file
Convert a string to an image
Convert to HSV
Convert (compress) formatted JSON string to 1-line JSON
Convert FBX files to ASCII <-> BINARY in Python
Convert binary packages for windows to wheel format
Convert pandas dataframe elements to regular string type
Convert 202003 to 2020-03 with pandas
Convert kanji to kana
Convert jupyter to py
Hexadecimal string and string conversion
Convert keras-yolo3 to onnx
Convert dict to array
Convert json to excel
How to convert / restore a string with [] in python
How to convert floating point numbers to binary numbers in Python
C> Convert decimal to hexadecimal> Do not use sprintf ()
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert HTML to text file
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert IP address to decimal
I want to convert an ISO-8601 character string to Japan time
Convert SDF to CSV quickly
[Caffe] Convert mean file from binary proto format to npy format
Convert genbank file to gff file
Convert python 3.x code to python 2.x
Convert to a string while outputting standard output with Python subprocess
Read big endian binary in Python and convert it to ndarray
Convert Tweepy Status object to JSON
Convert .ipynb to .html (with BatchFile)
Function to convert Excel column to number
Convert PDF to Documents by OCR
Convert markdown to PDF in Python
Convert A4 PDF to A3 every 2 pages
Convert wma to mp3 on Mac
Convert some Japanese names to antonyms
convert ggplot based graph to html
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Convert sentences to vectors with gensim
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
Convert from pdf to txt 2 [pyocr]
How to convert Tensorflow model to Lite
6 ways to string objects in Python
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
Efficient conversion method to byte string
Program to convert Japanese to station name
How to convert from .mgz to .nii.gz
Convert PDF to image with ImageMagick
A tool to convert Juniper config
Python> tuple> Convert double tuple to single tuple
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Note: [Python3] Convert datetime to a string in any format you like
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number