办法让这个十六进制数的值 [英] Way to get value of this hex number

查看:121
本文介绍了办法让这个十六进制数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进口binasciiF =开放('file.ext,RB)
打印binascii.hexlify(f.read(4))
f.close()

这将打印:


  

84010100


我知道我必须检索十六进制数184出这些数据。
怎么能在Python做呢?我以前用过的结构模块,但我不知道它的小尾数,big..whatever ..我怎样才能从这个号码使用结构得到184?


解决方案

 >>> X = B'\\ X84 \\ X01 \\ X01 \\ X00'
>>>进口结构
>>> struct.unpack_from('< H',X)
(388)
>>>地图(十六进制,struct.unpack_from('< H',X))
['量0x184']

< 表示小尾数, ^ h 表示读取16位整数()。细节是在包doc

import binascii

f = open('file.ext', 'rb')
print binascii.hexlify(f.read(4))
f.close()

This prints:

84010100

I know that I must retrieve the hex number 184 out of this data. How can it be done in python? I've used the struct module before, but I don't know if its little endian, big..whatever.. how can I get 184 from this number using struct?

解决方案

>>> x = b'\x84\x01\x01\x00'
>>> import struct
>>> struct.unpack_from('<h', x)
(388,)
>>> map(hex, struct.unpack_from('<h', x))
['0x184']

< means little endian, h means read a 16-bit integer ("short"). Detail is in the package doc.

这篇关于办法让这个十六进制数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆