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

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -