How can I write integers to a file in binary in Python 3?
For example, I want to write 6277101735386680763835789423176059013767194773182842284081 to a file in binary in exactly 24 bytes (unsigned, I will only be working with positive integers). How can I do this? I tried the following:
struct.pack("i", 6277101735386680763835789423176059013767194773182842284081)
This results in
ValueError: cannot fit 'int' into an index-sized integer
I have tried the same with some other formats ("l", "Q"), but those also result in errors:
struct.error: argument out of range
If I could convert the integer to exactly 24 bytes, I would be able to write to the file, since I know how to do that. However, I can't seem to convert the integers into bytes.
Also, how do I make sure that exactly 24 bytes are written per integer? I will also be writing smaller numbers (1000, 2000, 1598754, 12), however those should also take 24 bytes.
And how can I read the integers again from the file afterwards?
With Python 3 you can do the following:
i = 6277101735386680763835789423176059013767194773182842284081
with open('out.bin', 'wb') as file:
file.write((i).to_bytes(24, byteorder='big', signed=False))
with open('out.bin', 'rb') as file:
j = int.from_bytes(file.read(), byteorder='big')
print(j)
Output:
$ python3 tiny.py
6277101735386680763835789423176059013767194773182842284081
You can extract the least significant byte with
x = value & 0xFF
and you can remove that byte from the number with
value = value >> 8
Repeating this procedure 24 times will give you the bytes.
You can also speed up this process by taking out more bytes, for example with
x = value & 0xFFFFFFFF
value = value >> 32
you're processing 4 bytes at a time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.