Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created August 14, 2022 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonetheman/1972c4778e40fe7195247dd0df57c4e9 to your computer and use it in GitHub Desktop.
Save tonetheman/1972c4778e40fe7195247dd0df57c4e9 to your computer and use it in GitHub Desktop.
python3 struct read bytes
import struct
data = open("test.bin","rb").read()
# all of these are unsigned
def read_single_byte(data,position):
return struct.unpack("B",data[position:position+1])
def read_single_word(data,position):
return struct.unpack("H",data[position:position+2])
def read_single_int(data,position):
return struct.unpack("I",data[position:position+4])
print(read_single_byte(data,16))
print(read_single_word(data,16))
print(read_single_int(data,16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment