1

I have four bytes which I need to read as a long.

When I read my file, I read the following bytes:

['\x10', '\xef', '\xf1', '\xea']

According to my file format description, these four bytes represent a long.

How would I convert these bytes to a long with Python?

Additionally: Would one use a similar method to convert 2 bytes into a Short?

Here is example code of how I read my file:

source = "test.whatever"
print "Loading file:", source;
data = []   
f = open(source, "rb")
counter = 0;

#Let's load the data here:
try:
    byte = f.read(1) #skip the first byte according to my file
    while byte != "": 
        byte = f.read(1)
        data.append(byte)  #read every byte into an array to work on later
        counter = counter + 1

finally:
    f.close()
print "File loaded."
print counter
FredFury
  • 2,286
  • 1
  • 22
  • 27
  • 1
    See if your answer is here: http://stackoverflow.com/questions/444591/convert-a-string-of-bytes-into-an-int-python (talks about ints, but should apply). BTW `long` in Python is more like BigInt in Java, not like long. – Ray Toal Jan 19 '15 at 06:36
  • Thanks Ray- I'll check it out! – FredFury Jan 19 '15 at 06:37

1 Answers1

3
import struct
struct.unpack('>l', ''.join(ss))

I chose to interpret it as big-endian, you need to decide that yourself and use < instead if your data is little-endian.

If possible, use unpack_from() to read the data directly from your file, instead of using an intermediate list-of-single-char-strings.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • So maybe this is something to discuss as well: How the data is being read. I'll update my question with some example code. – FredFury Jan 19 '15 at 08:24
  • @FredFury: right, so you should stop reading one byte at a time, and instead read the largest chunks you can. And further, you can use `unpack_from()` to do the actual reading and avoid storing strings of data only to convert them to structs. – John Zwinck Jan 19 '15 at 08:54
  • So I've been looking into this. The advantage of using an array is that you can index the bytes, which helps when you need to read a set of bytes at a location. Is there a better way of doing this? – FredFury Jan 19 '15 at 10:26
  • Well you can read the whole file into a string if you need to, just do it at once instead of one byte at a time into a list. A string can be indexed. – John Zwinck Jan 19 '15 at 13:30