How to use Python bytes

How to use bytes

Confirmed with Python 3.7.5.

Bytes self-input

b'\x00\x01\x02\x03'
# result: b'\x00\x01\x02\x03'

b'\x64\x65\x66\x67'
# result: b'defg' #Characters corresponding to ascii code are displayed

Read from file with bytes


#without with
fp = open('filename.bin', 'rb')
all_bytes = fp.read()
fp.close()

#With with
with open('filename.bin', 'rb') as fp:
    all_bytes = fp.read()

Convert integers to bytes

a = 255  #Preparation code
a.to_bytes(2, 'little')  # to_bytes(Number of bytes after conversion,Endian)
# result: b'\xff\x00'

From bytes to integer

a = 255                         #Preparation code
byts = a.to_bytes(2, 'little')  #Preparation code
int.from_bytes(byts, 'little')  # int.from_bytes(bytes,Endian)
# result: 255

From bytes to integer (signed)

a = -255                                     #Preparation code
byts = a.to_bytes(2, 'little', signed=True)  #Preparation code
int.from_bytes(byts, 'little', signed=True)  # int.from_bytes(bytes,Endian, signed=True)
# result: -255

From hexadecimal string to bytes

bytes.fromhex('F1E2f3f4')
bytes.fromhex('F1E2 f3f4')
bytes.fromhex('F1 E2 f3 f4')
# result: b'\xf1\xe2\xf3\xf4'

bytes to hexadecimal notation string

by = bytes.fromhex('F1E2f3f4')  #Preparation code
by.hex()
# result: 'f1e2f3f4'

Recommended Posts

How to use Python bytes
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
How to use xml.etree.ElementTree
How to use Python-shell
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to install and use pandas_datareader [Python]
How to use numpy.vectorize
How to use pytest_report_header
[python] How to use __command__, function explanation
How to install python
How to use partial
How to use Bio.Phylo
How to use SymPy
[Python] How to use import sys sys.argv
How to use x-means
How to use WikiExtractor.py
How to use IPython
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
How to use iptables
python: How to use locals () and globals ()
How to use numpy
How to use __slots__ in Python class
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use "deque" for Python data
How to use python-kabusapi