Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let python do the endian stuff #1075

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions PIL/_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# See the README file for information on usage and redistribution.
#

from struct import unpack, pack

if bytes is str:
def i8(c):
return ord(c)
Expand All @@ -34,7 +36,7 @@ def i16le(c, o=0):
c: string containing bytes to convert
o: offset of bytes to convert in string
"""
return i8(c[o]) | (i8(c[o+1]) << 8)
return unpack("<H", c[o:o+2])


def i32le(c, o=0):
Expand All @@ -44,33 +46,31 @@ def i32le(c, o=0):
c: string containing bytes to convert
o: offset of bytes to convert in string
"""
return (i8(c[o]) | (i8(c[o+1]) << 8) | (i8(c[o+2]) << 16) |
(i8(c[o+3]) << 24))
return unpack("<I", c[o:o+4])


def i16be(c, o=0):
return (i8(c[o]) << 8) | i8(c[o+1])
return unpack(">H", c[o:o+2])


def i32be(c, o=0):
return ((i8(c[o]) << 24) | (i8(c[o+1]) << 16) |
(i8(c[o+2]) << 8) | i8(c[o+3]))
return unpack(">I", c[o:o+4])


# Output, le = little endian, be = big endian
def o16le(i):
return o8(i) + o8(i >> 8)
return pack("<H", i)


def o32le(i):
return o8(i) + o8(i >> 8) + o8(i >> 16) + o8(i >> 24)
return pack("<I", i)


def o16be(i):
return o8(i >> 8) + o8(i)
return pack(">H", i)


def o32be(i):
return o8(i >> 24) + o8(i >> 16) + o8(i >> 8) + o8(i)
return pack(">I", i)

# End of file