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

little/big endian #86

Open
hikavdh opened this issue Nov 2, 2022 · 2 comments
Open

little/big endian #86

hikavdh opened this issue Nov 2, 2022 · 2 comments
Labels

Comments

@hikavdh
Copy link

hikavdh commented Nov 2, 2022

When I read a word from i2c, it is treated as big endian. However I want to read a temperature that is stored as little endian. I have solved this so:

BUS = SMBus(1)
value = BUS.read_word_data(address, register)
hstr = str(hex(value))[2:].rjust(4, '0')
value = int('0x%s%s' % (hstr[2:], hstr[:2]), 16)

Not very elegant.
Is there a native way to handle this?

@kplindegaard
Copy link
Owner

Slow response, sorry. but you don't need to go via strings.

little_endian_value = ((value & 0xFF) << 8) | ((value >> 8) & 0xFF)

@hikavdh
Copy link
Author

hikavdh commented May 17, 2023

Thanks!

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

No branches or pull requests

2 participants