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

Use int.from_bytes() instead of struct for various ints (u1, u2, u4, etc.) #78

Open
armijnhemel opened this issue Dec 15, 2023 · 1 comment

Comments

@armijnhemel
Copy link

I am filing this issue as something to possibly consider for the future.

Currently struct is used for converting bytes to ints (and a few other values like floats, but this issue is only about ints):

https://github.com/kaitai-io/kaitai_struct_python_runtime/blob/master/kaitaistruct.py#L170

For ints Python 3 has a different mechanism for converting from byte strings to ints namely from_bytes():

https://docs.python.org/3/library/stdtypes.html#int.from_bytes

What is very convenient is that it allows arbitrary length byte strings, so it becomes absolutely trivial to implement something like u3 or u5 or u11.

@armijnhemel
Copy link
Author

Of course, if you are want to keep Python 2 support, then you would need to keep the current solution. I don't know if other languages support something like this, but if so, then dropping Python 2 support would be a small price to pay.

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

1 participant