How do I write a long integer as binary in Python?

后端 未结 9 1336
梦谈多话
梦谈多话 2021-01-12 06:20

In Python, long integers have unlimited precision. I would like to write a 16 byte (128 bit) integer to a file. struct from the standard library supports only u

9条回答
  •  萌比男神i
    2021-01-12 06:43

    Why not use struct with the unsigned long long type twice?

    import struct
    some_file.write(struct.pack("QQ", var/(2**64), var%(2**64)))
    

    That's documented here (scroll down to get the table with Q): http://docs.python.org/library/struct.html

提交回复
热议问题