binaryparse icon indicating copy to clipboard operation
binaryparse copied to clipboard

[Question] Unexpected results

Open mantielero opened this issue 4 years ago • 3 comments

I have the following in python:

import struct

a = 0x95006c08 
print( a & 0x3FF )           # First: 10 bits  --> 8
print( (a >> 10) & 0x1FFF )  # Second: 13 bits --> 27

And I am trying to do the equialent with binaryparse:

import binaryparse
import streams

createParser(p):
  10: a
  13: b 

var 
  a = 0x95006C08 
  s = newStringStream()

s.write(a)
s.setPosition(0)

var tmp = p.get(s)
echo tmp.a  # --> 33 (instead of 8)
echo tmp.b  # --> 2688 (instead of 27)

What am I doing wrong? I am looking to replicate python's results.

By the way, binaryparse is AWESOME. I love it.

mantielero avatar Mar 24 '21 17:03 mantielero

There are a couple issues here. First s.write(a) will write a with the wrong byte order from what you seem to expect. If you create a parser that has 32: a and then put the value through that into the stream it will be in the same order as binaryparse expects it to be. Second of is that binaryparse reads from left to right so to speak. So you need to skip the first 9 bits before you can read first the 13 and then the 10. But while looking into this I've discovered that binaryparse has two bugs. I've pushed a fix for the first one, so what I wrote above should work now. But the other one is a bit harder to squash, so you might run into some strange behaviour.

To be honest binaryparse could do with a complete rewrite, the code is complete spaghetti and super hard to debug.. But I'll try to patch it over for now.

PMunch avatar Mar 24 '21 21:03 PMunch

But @PMunch... there is already a rewrite :3

sealmove avatar Mar 27 '21 13:03 sealmove

I wasn't aware of the existance of binarylang. It actually solved my problem. Thanks for the info.

mantielero avatar Mar 27 '21 17:03 mantielero