bitstring icon indicating copy to clipboard operation
bitstring copied to clipboard

Add length argument to read()

Open GoogleCodeExporter opened this issue 10 years ago • 3 comments

I'm not sure if it will clobber the API, but I think it would be nice if read() 
would accept an additional argument to specify the length.

When you parse binary files, you often have your data prefixed with the size of 
it. So you need to read out the size first, and then the data.

Currently you would do:
    datasize = s.read('int:12')
    data = value.read(8*datasize).bytes

or 

    data = value.read('bytes:{}'.fomrat(datasize))

It would be nice if you could do:

    data = value.read('bytes', datasize))

Cheers,
  Volker

Original issue reported on code.google.com by [email protected] on 30 Mar 2012 at 2:57

GoogleCodeExporter avatar Apr 11 '15 20:04 GoogleCodeExporter

Hi,

I'm not sure that I see what the additional parameter would gain you. As you 
say, instead of 

    datasize = s.read('int:12')
    data = s.read('bytes', datasize)

you can use

    data = s.read(8*datasize).bytes

or

    data = s.read('bytes:{}'.format(datasize))

so I don't think you get much extra (you still need to read the datasize 
separately), and I don't like to add things the the API if I can avoid it.

On the other hand, what I think would be nice is to allow something like this:

    datasize, data = s.readlist('int:12=a, bytes:a')

which would allow the whole thing to happen in one step.

Original comment by [email protected] on 30 Mar 2012 at 3:11

GoogleCodeExporter avatar Apr 11 '15 20:04 GoogleCodeExporter

This

  datasize, data = s.readlist('int:12=a, bytes:a')

would definitely be the nicest of all.

Original comment by [email protected] on 30 Mar 2012 at 3:17

GoogleCodeExporter avatar Apr 11 '15 20:04 GoogleCodeExporter

See also issue 131 for a slightly different usage, for example 
s.readlist('int:12=a, a*uint:8')

Original comment by [email protected] on 9 Dec 2012 at 4:07

GoogleCodeExporter avatar Apr 11 '15 20:04 GoogleCodeExporter