Does python has similar function as pack() in php?

104 views Asked by At

In php, pack("V", $id); It can change the "id" into binary with the 32 bits , unsigned, little endian format. How can we do the same this using python?

Another thing, I try the following code:

my_input = 10
binary_string = pack("<I", my_input)
print my_input

The output is :

▯▯▯

what is wrong with my code or my pycharm ?

1

There are 1 answers

0
Robᵩ On

To create a 32-bit unsigned little endian memory region in Python, use struct.pack just as you have:

binary_string = pack("<I", my_input)

You can then store those bytes in a file like so:

with open("my_input.bin", "wb") as f:
    f.write(binary_string)