Python need help understanding bit wise and byte manipulation unexpected behaviour

只愿长相守 提交于 2019-12-11 16:29:19

问题


I'm trying to build up a packet (adaption field)

I'm trying to build the PCR packet, but im a bit confused on python bitwise operators....

adapt_header_3 = (0x1f45db5e4df << (9 + 6)) # adapt_header_3 = 2149055849695

# d5 7d 51 05 7f 27 - This is what the correct result should look like


print(hex(adapt_header_3)) ## << but im getting this for adapt_header_3 0xfa2edaf26f8000

So I expect:

d5 7d 51 05 7f 27 (6 bytes)

But i'm getting:

fa 2e da f2 6f 80 00 00 (8 bytes)

UPDATE: Bit more of a test im using this:

adapt_header_1 = 0x7 
adapt_header_2 = 0x10
adapt_header_3 = 0x1f45db5e4df


adapt_header = 0x0
adapt_header = adapt_header | (adapt_header_1 << (48 + 8))
adapt_header = adapt_header | (adapt_header_2 << (48))
adapt_header = adapt_header | (adapt_header_3 << (9 + 6)) 

print(hex(adapt_header))

To build this:

来源:https://stackoverflow.com/questions/58657928/python-need-help-understanding-bit-wise-and-byte-manipulation-unexpected-behavio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!