python - Get nth byte of integer -


i have following integer:

target = 0xd386d209 print hex(target) 

how can print nth byte of integer? example, expected output first byte be:

0x09 

you can of bit manipulation. create bit mask entire byte, bitshift mask number of bytes you'd like. mask out byte using binary , and bitshift result first position:

target = 0xd386d209 n = 0 # 0th byte goal = 0xff << (8 * n) print hex((target & goal) >> (8 * n)) 

you can simplify little bit shifting input number first. don't need bitshift goal value @ all:

target = 0xd386d209 n = 0 # 0th byte goal = 0xff print hex((target >> (8 * n)) & goal) 

Comments

Popular posts from this blog

xml - Extract substrings with XSLT -

math - "ELO Rating" and how does "TSR Rating" work? -

How can do a tuple comparison in coffeescript similar to that in python? -