Python: Writing Int To A Binary File
I have a program which calculates the offset(difference) and then stores them in an 16 bit unsigned int using numPy and I want to store this int into a binary file as it is in bina
Solution 1:
Try this.
import numpy as np
a=int(4)
binwrite=open('testint.in','wb')
np.array([a]).tofile(binwrite)
binwrite.close()
b=np.fromfile('testint.in',dtype=np.int16)
print b[0], type(b[0])
output: 4 type 'numpy.int16'
I Hope this is wha you are looking for. Works for n>127 But read and writes numpy arrays... binwrite=open('testint.in','ab') will let you append more ints to the file.
Solution 2:
You should use the built-in struct
module. Instead of this:
np.uint16(Current-Start)
Try this:
struct.pack('H', Current-Start)
Post a Comment for "Python: Writing Int To A Binary File"