简体   繁体   English

Python pop和hex值

[英]Python pop and hex values

I'm new to python and really need some help. 我是python新手,确实需要一些帮助。 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. 因此,我可以从测试文件中正确获取21229的整数值(十六进制值0x52ed),但是我需要进一步采取措施,仅将前两位拆分为十六进制(0x52),这样我才能得到整数值84,即我所追求的值。 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

To go from 0x52ed to 0x52 you can shift right by 8 bits: 要从0x52ed变为0x52,您可以右移8位:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM