-5

Possible Duplicate:
reading integers from binary file in python

I've read the solution to a similar problem here: convert a string of bytes into an int (python) but I'm not quite sure how to repurpose it for my needs.

I have a .bin file which is just a sequence of bytes. Every set of 4 bytes represents a 32-bit number. I am trying to use the struct module as described in that linked question to convert every set of 4 bytes to an integer and print them to a new file. How can I achieve this?

Thanks for the help.

Community
  • 1
  • 1
NickHalden
  • 1,469
  • 2
  • 20
  • 31
  • 1
    Clearly it's not that ambiguous since Steve understood and answered it with exactly what I was looking for within minutes after I posted the question. In any case, go ahead and close if you would like since I got what I was looking for. – NickHalden May 14 '12 at 00:48

1 Answers1

1

You might want to read Read ints from file in python It is even more straightforward from that question.

I have not checked the following code but something along the spirit of

fin = open("hi.bmp", "rb")
out = open("values.txt","rw")
value = struct.unpack('i', fin.read(4))[0]
out.write("%d\n" % value) # just loop over the 2 last lines
out.close()
fin.close()

should do the trick, if you want to record the ints as readable integers in another file.

Community
  • 1
  • 1
Steve K
  • 10,879
  • 4
  • 39
  • 39