发布于 2015-09-11 16:21:59 | 137 次阅读 | 评论: 0 | 来源: 网络整理

#
# The Python Imaging Library.
# $Id$
#
# Binary input/output support routines.
#
# Copyright (c) 1997-2003 by Secret Labs AB
# Copyright (c) 1995-2003 by Fredrik Lundh
# Copyright (c) 2012 by Brian Crowell
#
# See the README file for information on usage and redistribution.
#

if bytes is str:
    def i8(c):
        return ord(c)

    def o8(i):
        return chr(i&255)
else:
[docs] def i8(c): return c if c.__class__ is int else c[0]
[docs] def o8(i): return bytes((i&255,)) # Input, le = little endian, be = big endian #TODO: replace with more readable struct.unpack equivalent
[docs]def i16le(c, o=0): """ Converts a 2-bytes (16 bits) string to an integer. c: string containing bytes to convert o: offset of bytes to convert in string """ return i8(c[o]) | (i8(c[o+1])<<8)
[docs]def i32le(c, o=0): """ Converts a 4-bytes (32 bits) string to an integer. 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)
[docs]def i16be(c, o=0): return (i8(c[o])<<8) | i8(c[o+1])
[docs]def i32be(c, o=0): return (i8(c[o])<<24) | (i8(c[o+1])<<16) | (i8(c[o+2])<<8) | i8(c[o+3]) # Output, le = little endian, be = big endian
[docs]def o16le(i): return o8(i) + o8(i>>8)
[docs]def o32le(i): return o8(i) + o8(i>>8) + o8(i>>16) + o8(i>>24)
[docs]def o16be(i): return o8(i>>8) + o8(i)
[docs]def o32be(i): return o8(i>>24) + o8(i>>16) + o8(i>>8) + o8(i)
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务