https://www.devdungeon.com/content/working-binary-data-python
Overview
- The Bytes Type
- The Bytearray Type
- Reading Bytes From a File
- Integer to Bytes
- Bytes to Integer
- Text Encoding and Decoding
- Base 64 Encoding and Decoding
- Hexadecimal
- Format Strings
- Bitwise Operations
- Struct Packing and Unpacking
- System Byte Order
- Examples
- diff.py - Do two files match?
- is_jpeg.py - Does the file have a JPEG binary signature?
- read_boot_sector.py - Inspect the first 512 bytes of a file
- find_ascii_in_binary.py - Identify ASCII characters in binary files
- create_stego_zip_jpg.py - Hide a ZIP archive in a JPEG
- extract_pngs.py - Extract PNGs from a file and store them in a pngs/ directory.
All examples are in Python 3.
The Bytes Type
The bytes type in Python is immutable and stores a sequence of values ranging from 0-255 (8-bits). You can get the value of a single byte by using an index like an array, but the values can not be modified.
# Create empty bytes