Merging byte arrays

Gabriel dunmer at dreams.sk
Sat Apr 11 10:02:25 EDT 2009


Hello
I'm using this function to read data in byte format from file

def readBytes(file, offset, size):
    file.seek(offset)
    return file.read(size)

file is opened with open function:

file = open(path, "rb")

then i'm using array.array('B', bytes) to parse read-out  data, for 
example in this function:

def getIntValOfBytes(bytes):
    value = i = 0
    for byte in array.array('B', bytes):
        value |= byte << (8 * i)
        i += 1
           
    return value

when i read one portion of bytes everything works ok. but now i need 
read from more locations in file, then somehow merge these data together 
and then pass to getIntValOfBytes function..

i tried list

list = []
for offset in offsets
     list.extend(readBytes(file, offset, size))
  
or string

string = ""
for offset in offsets
     string += readBytes(file, offset, size)

but with list i get this error

    for byte in array.array('B', bytes):
TypeError: an integer is required

in getIntValOfBytes (i passed the part of the list to the method, list[x:y])

and with string it just freeze....

I will appreciate any help..
[I'm newbie so sorry if i'm missing something obvious]



More information about the Python-list mailing list