Skip to content

[Question] Unexpected results #17

Description

@mantielero

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.

Activity

  1. PMunch commented on Mar 24, 2021

    @PMunch
    Owner

    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.

  2. sealmove commented on Mar 27, 2021

    @sealmove
    Contributor

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

  3. mantielero commented on Mar 27, 2021

    @mantielero
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions