Created
August 14, 2022 00:18
-
-
Save tonetheman/1972c4778e40fe7195247dd0df57c4e9 to your computer and use it in GitHub Desktop.
python3 struct read bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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