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?