How to Convert Raw Big-Endian Values in Python

How to Convert Raw Big-Endian Values in Python

PSD files store most binary fields in big-endian byte order. When you need to decode a raw field yourself — for example, from an image resource’s raw bytes — BigEndianBitConverter reads a big-endian 32-bit integer directly from a byte array.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-psd-foss/Aspose.PSD-FOSS-for-Python.git
cd Aspose.PSD-FOSS-for-Python
# No packaging manifest is published yet -- run from this folder,
# or add it to PYTHONPATH:
export PYTHONPATH=$PWD:$PYTHONPATH

Step 2: Import Required Classes

from aspose_psd_foss.psdimage import PsdImage
from aspose_psd_foss.bigendianbitconverter import BigEndianBitConverter

Step 3: Read a Resource’s Raw Bytes

PsdImage.parsed_resources returns the lower-level UnknownResource objects, each exposing the raw payload via data:

image = PsdImage.load("input.psd")

for resource in image.parsed_resources:
    raw = resource.data

Step 4: Decode a 32-Bit Big-Endian Value

data = bytes([0x00, 0x00, 0x04, 0x10])  # example 4-byte big-endian field
value = BigEndianBitConverter.to_int32(data, 0)
print(f"Decoded value: {value}")  # 1040

to_int32(byte_array, offset) reads 4 bytes starting at offset and combines them most-significant-byte-first.

Common Issues and Fixes

IndexError when calling to_int32. The byte array must have at least 4 bytes available starting at offset — check len(byte_array) >= offset + 4 before calling.

Values look byte-swapped. Confirm you’re not accidentally using a little-endian conversion elsewhere in your own code — PSD resource and header fields are consistently big-endian, which is exactly what BigEndianBitConverter assumes.

Frequently Asked Questions

Does BigEndianBitConverter support other integer sizes?

In this FOSS build, BigEndianBitConverter provides to_int32 only — for 16-bit or 64-bit big-endian values, decode the bytes manually with Python’s built-in int.from_bytes(data, byteorder="big").

Do I need this for normal document editing?

No — BigEndianBitConverter is a low-level utility for working with raw bytes you’ve obtained yourself (for example, from an unrecognized image resource). Typical loading, editing, and saving (see Working with the Core API) never requires it directly.

See Also