user2377057
user2377057

Reputation: 183

Python pop and hex values

I’m new to python and really need some help. I’m opening a file in binary and looking for a specific offset then reading the first couple of bytes at the offset. I’m struggling to understand how I can further isolate the value I need from within the returned string. So, I can correctly get a integer value of 21229 from the test file (which is hex value 0x52ed), but I need to take a step further and split the hex for only the first two bits (0x52), so that I get the integer value 84, which is the value I’m after. Code is below. Many thanks for any help given

offset=0x49f2
from array import *
import os,sys
def emptyarray(data):
    while len(data) > 0:
       data.pop()

bo=sys.byteorder
filename="c:\\test\test.fil"
fp=open (filename,"rb")
data=array("h")
fp.seek(offset,0)
if bo=="big":
   data.byteswap()
data.fromfile(fp,1)
value=data.pop()
print value
hexvalue=hex(value)
print hexvalue

Upvotes: 0

Views: 205

Answers (1)

Janne Karila
Janne Karila

Reputation: 25207

To go from 0x52ed to 0x52 you can shift right by 8 bits:

>>> hex(0x52ed >> 8)
'0x52'

Upvotes: 1

Related Questions