Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different lengths compared to Python's struct.calcsize #13

Open
mikew opened this issue Apr 7, 2019 · 1 comment
Open

Different lengths compared to Python's struct.calcsize #13

mikew opened this issue Apr 7, 2019 · 1 comment

Comments

@mikew
Copy link

mikew commented Apr 7, 2019

For a string like BBBBBBBh, jspack and Python give different sizes:

FMT="BBBBBBBh"
$ node -e "console.log(require('jspack').jspack.CalcLength('$FMT'))"
9
$ python -c "import struct; print(struct.calcsize('$FMT'))"
10
@dgarciabriseno
Copy link

This is expected behavior.

Per the readme:

Character | Byte Order
----------------------------------
    <     | little-endian
    >     | big-endian
    !     | network (= big-endian)

If the first character is not one of these, "!" is assumed.

This is different than Python's default choice

If the first character is not one of these, '@' is assumed.

The difference between these is that '@' is native byte order. System dependent. And it's not supported by jspack. The python docs also say

Padding is only automatically added between successive structure members. No padding is added at the beginning or the end of the encoded struct.

Padding i being added for the h because it sites at byte 7. When using the native format, Python wants to put the short at byte 8.

>>> struct.calcsize('BBBBBBBh')
10
>>> struct.calcsize('<BBBBBBBh')
9
>>> struct.calcsize('!BBBBBBBh')
9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants