Merging byte arrays

MRAB google at mrabarnett.plus.com
Sat Apr 11 10:36:13 EDT 2009


Gabriel wrote:
> 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])
> 
That's because you're passing a list of strings.

It should for a list of integers or a string.

> and with string it just freeze....
> 
'Freezes'? I've tried it with a string of 256 characters and it was
fast.

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




More information about the Python-list mailing list