DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Reading Bytes Value

09.18.2005
| 19925 views |
  • submit to reddit
        
from struct import unpack
f = open('data.bin', 'rb')

b = f.read(1)
b2 = f.read(2)
b4 = f.read(4)

n = ord(b)  # from char to int
n4 = unpack('>L', b4)  # big endian
n4 = unpack('<L', b4)  # little endian
n = unpack('>h', b2)   # big endian short