Interpret Bytes as Packed Binary Data

Project #1

Read each BMP test file.

Hex dump the beginning of the files to see the raw (packed) byte data.

Display the following structured (packed) binary data (metadata)

To see code that can interpret/display BMP file header data, click HERE .

Project #2

Do other file formats contain structured (packed) byte data? Files such as png, gif, mp3, doc, docx, ttf, bin, etc?

Analyze and extract the metadata (if any) from a binary file and display it. (Do not display everything, but display at least 4 or more basic metadata.)

Project #3

Locate, unpack, and display the metadata of the structured (binary) data such as TCP/IP packets, etc.

BMP Test Files

BMP Test File #1
BMP Test File #2
BMP Test File #3

BMP files are little-endian.

The ID field is the first two bytes of a BMP file. They must be (0x42 and 0x4D) or "BM".

Metadata Definition

Metadata is data about data. For example, a BMP file contain image data. It also contain metadata about the image data. For example, image height, image width, etc.

Structural metadata describes the structure of an object. An example of structural metadata is the organization of multiple pages as chapters in a book.

Metadata (Wikipedia)

Big or Little Endian

Endian refers to the order in which bytes are stored. With big endian, the most significant byte is sent first. With little endian, the least significant byte is sent first.

For example a 16 bit integer consists of two bytes. Endian determines which byte is first, and which is second.

To make things more complicated, the Ethernet protocol while it uses little endian at the bit level, uses big endian at the byte level.

Code Examples

# read a file as binary (byte) data with open('project_221_01.bmp','rb') as file: byte_data = file.read() return byte_data

# unpack Binary (struct) data import struct print(f'Type: {byte_data[0:2].decode()}') print(f'Size: {struct.unpack('I',byte_data[2:6])}')

# combine example 1 & 2 import struct with open('project_221_01.bmp', 'rb') as file: print(f'Type: {file.read(2).decode()}') print(f'Size: {struct.unpack('I',file.read(4))}')

Links

BMP file format (Wikipedia)

Byte (Wikipedia)

Endianness (Wikipedia)

struct - Interpret bytes as packed binary data (Python Doc)

PNG (Wikipedia)

How to Read BMP file Header in Python