-1

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 ?

ettanany
  • 19,038
  • 9
  • 47
  • 63
Jacob_C
  • 25
  • 1
  • 5
  • 3
    Possible duplicate of [Python equivalent of php pack](http://stackoverflow.com/questions/13892734/python-equivalent-of-php-pack) – Jeroen Heier Dec 22 '16 at 20:07
  • Are you sure that was the output of *that precise program*? The output should have been `10`. Please don't retype or summarize your code, please copy-paste a short, complete program, including any import statements. See [mcve] for helpful information. – Robᵩ Dec 22 '16 at 20:35
  • Let me introduce you to your new best friend: http://www.php2python.com/wiki/function.pack/ – Sammitch Dec 22 '16 at 21:18

1 Answers1

1

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)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308