Python - Packing string representing short int into 2 byte string

491 views Asked by At

I do some sort of calculation and in the end I have an int that needs at most 16 bits to represent. I want to pack it into a string in unsigned short int format. For example, if I have 1223, I want to store 0000010011000111.
I tried using:

n = pack('H', 1223)

When I tried to print in (in binary representation) I got:

11000111 100

But I want the leading zeros to also be encoded into n, how can I do it elegantly?

0

There are 0 answers