I'm trying to read the values from a binary file but I'm having some trouble. This is what I'm doing:
from struct import unpack
with open("pixelValues.txt", "rb") as f:
byte = f.read(8)
foo = unpack("<Q", byte)
print(foo)
When I run the program the output is (4244912790557L,)
which doesn't make sense to me because it should be 1485102109
. Does anyone see what I'm doing wrong?
Here is a screenshot of the file:
You're reading too much. Change f.read(8)
to f.read(4)
and change unpack("<Q", byte)
to unpack("i", byte)
and that will fix your problem.
pack('<Q', 1485102109)
Returns:
'\x1d\xdc\x84X\x00\x00\x00\x00'
Which is not consistent with your file. How did you write it?
Edit:
You have written the number with a %d specifier, which means that you've written it as a 4-byte integer, not as an unsigned long long, but as an unsigned int. You should read it like this:
from struct import unpack
with open("pixelValues.txt", "rb") as f:
num = f.read(4)
foo = unpack("<I", num)
print(foo)
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.