assume i have to store few integer numbers like 1024 or 512 or 10240 or 900000 in a file, but the condition is that i can consume only 4 bytes (not less nor max).but while writing a python file using write method it stored as “1024” or “512” or “10240” ie they written as ascii value but i want to store directly their binary value.
Any help will really appreciable.
use the struct module
In Python3, it you can use the
to_bytesmethod of int. The paren around 1024 are only necessary as 1024. parses as a float and would cause a syntax error.