Lenard Lindstrom avatar Lenard Lindstrom committed c780a5d

Allow switching the byte order of a Python int to big or little endian

The pygame.tests.test_utils.endian module does machine independent
byte reorder of the first 4 bytes of a Python int.

Comments (0)

Files changed (1)

test/test_utils/endian.py

+# Module pygame.tests.test_utils.endian
+#
+# Machine independent conversion to little-endian and big-endian Python
+# integer values.
+
+import struct
+
+def little_endian_uint32(i):
+    """Return the 32 bit unsigned integer little-endian representation of i"""
+
+    s = struct.pack('<I', i)
+    return struct.unpack('=I', s)[0]
+
+def big_endian_uint32(i):
+    """Return the 32 bit unsigned integer big-endian representation of i"""
+
+    s = struct.pack('>I', i)
+    return struct.unpack('=I', s)[0]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.