binaryparse
binaryparse copied to clipboard
[Question] Unexpected results
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.
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.
But @PMunch... there is already a rewrite :3
I wasn't aware of the existance of binarylang. It actually solved my problem. Thanks for the info.